0

I am creating a website in which , I am inserting data into database using ajax XMLHttpRequest().

From php file I am sending response like this.

$response = array();
$response["success"] = 1;
echo json_encode($response);

In javascript file I am getting response as.

var json = xmlhttp.responseText;

Now while executing, var json contains response with some additional texts

{"success":1}< !--first comments --> < script> an script from server < /script>< !--second comments -->

I want to parse only json text {"success":1} using javascript, plz don't suggest jquery approach.

I tried var obj = JSON.parse(json);, but it did not work.

2
  • 1
    after your echo ends echo json_encode($response); add exit; Commented Mar 20, 2014 at 8:00
  • thanks it worked , now php file returns only {"success":1} . Commented Mar 20, 2014 at 8:08

1 Answer 1

2

Change your ajax code to this

$response = array();
$response["success"] = 1;
echo json_encode($response);
exit;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.