admin管理员组

文章数量:1317906

I have a status 200 being returned when I'm using jQuery AJAX. However, I am also getting a syntax error from somewhere. I am posting to PHP like this:

function submit_order(orderInformation) {
    $.ajax({
        type: 'post',
        url: 'queries/submit_order.php?<?=time();?>',
        data: 'orderInformation=' + JSON.stringify(orderInformation),
        dataType: 'json',
        success: function (returnedData) {
            console.log(returnedData);
            $('#content_container').fadeOut(340, function () {
                var new_content = $('#content_container').clone(false);
                $('#content_container').remove();
                new_content.css('display', 'none');
                new_content.children().remove();

                new_content.appendTo('body');
                $('#content_container').vkTemplate('templates/confirm_template.tmpl?<?=time()?>', returnedData, function (el, data, context) {
                console.log('success'); 

                    $('#content_container').fadeIn(340);
                });
            });
        },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      }
    });
}

My PHP code is pretty straightforward:

$order_information = json_decode($json_str, true);

//go through the array and make an email out of it
//add a few elements to the array
//send the email

//send back a json string with the added elements
echo json_encode($order_information);

Yet I get this:

And oddly, if I copy paste the JSON string from console.log(JSON.stringify(orderInformation)) into the PHP page:

$json_str = '{"sector_0":{"file":[],"sector_info":{"sector_label":"NIO","purchase_order":"test","proof":false},"lines":{"line_0":{"description":"test","quantity":"2","productId":"1","addressId":"20","shipViaId":"1","notes":false}}}} ';

everything works. What is this error? Where could this < seen in the error be ing from?

Thanks

I have a status 200 being returned when I'm using jQuery AJAX. However, I am also getting a syntax error from somewhere. I am posting to PHP like this:

function submit_order(orderInformation) {
    $.ajax({
        type: 'post',
        url: 'queries/submit_order.php?<?=time();?>',
        data: 'orderInformation=' + JSON.stringify(orderInformation),
        dataType: 'json',
        success: function (returnedData) {
            console.log(returnedData);
            $('#content_container').fadeOut(340, function () {
                var new_content = $('#content_container').clone(false);
                $('#content_container').remove();
                new_content.css('display', 'none');
                new_content.children().remove();

                new_content.appendTo('body');
                $('#content_container').vkTemplate('templates/confirm_template.tmpl?<?=time()?>', returnedData, function (el, data, context) {
                console.log('success'); 

                    $('#content_container').fadeIn(340);
                });
            });
        },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      }
    });
}

My PHP code is pretty straightforward:

$order_information = json_decode($json_str, true);

//go through the array and make an email out of it
//add a few elements to the array
//send the email

//send back a json string with the added elements
echo json_encode($order_information);

Yet I get this:

And oddly, if I copy paste the JSON string from console.log(JSON.stringify(orderInformation)) into the PHP page:

$json_str = '{"sector_0":{"file":[],"sector_info":{"sector_label":"NIO","purchase_order":"test","proof":false},"lines":{"line_0":{"description":"test","quantity":"2","productId":"1","addressId":"20","shipViaId":"1","notes":false}}}} ';

everything works. What is this error? Where could this < seen in the error be ing from?

Thanks

Share Improve this question edited Jul 29, 2015 at 21:03 Salman Arshad 272k84 gold badges443 silver badges534 bronze badges asked Dec 13, 2012 at 18:23 12527481252748 15.4k34 gold badges117 silver badges241 bronze badges 17
  • 6 200 is not an error. It's an OK response from the webserver. – Codeguy007 Commented Dec 13, 2012 at 18:25
  • Did you try to open queries/submit_order.php?<?=time();?> in a browser or check with firebug what is exactly returned by a server? Looks like you have a syntax error or something, and html is returned – E_p Commented Dec 13, 2012 at 18:26
  • @Codeguy007 then why is printing it to the log being triggered on the error callback? Thanks! – 1252748 Commented Dec 13, 2012 at 18:27
  • 1 <?=time();?> definitely doesn't work as you can't run php clientside. – Codeguy007 Commented Dec 13, 2012 at 18:31
  • 1 So did check what is returned by your server? Raw response. – E_p Commented Dec 13, 2012 at 18:48
 |  Show 12 more ments

3 Answers 3

Reset to default 4

It is your error handler that gets fired and logs:

  • xhr.status (200)
  • thrownError (the syntax error)

Note that $.ajax with dataType: json will fire the error handler even if the server returns 200 OK but the response is invalid JSON. The syntax error is not in your JavaScript code but in the JSON. Identify where the < is ing from and make sure that your PHP script is sending valid JSON.

Tip: open the console and look at the network tab; all XHRs are logged there along with headers and body.

200 - Is an Ok response by a server http://www.w3/Protocols/rfc2616/rfc2616-sec10.html

You have a syntax error in your response server returns invalid json

As your PHP code seams fine, there must be something else. Syntax error or your framework returns json wrapped in html ...

Use proper tools to see what is returned by server. (firebug on firefox/ developer tools on chrome)

In your image you see 0: "<" That means that returned string starts with < - That means it is html that got returned.

Looks like you use chrome. Go to your "network" tab in chrome an you should be able to see raw response for your request.

so it is a php error:

$sector_index is not itarable. Can you var_dump it to see. what it is?

It looks like <?=time()?> isn't getting processed. Alert the URL before you post to it for verification.

本文标签: javascriptjQuery AJAX 200 statusyet mysterious syntax errorStack Overflow