Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
after your echo ends echo json_encode($response); add exit; –  Swapnil Mar 20 at 8:00
    
thanks it worked , now php file returns only {"success":1} . –  Hemant Mar 20 at 8:08

1 Answer 1

Change your ajax code to this

$response = array();
$response["success"] = 1;
echo json_encode($response);
exit;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.