简体   繁体   中英

JQuery.getJSON not alerting json

I currently running in localhost:

$.getJSON("http://localhost/call", function(json) {
  alert(json.something);
});

http://localhost/call returns {something:1} , yet nothing is alerted.

{something:1}

Isnt a valid JSON string, however

{"something":1}

is.

If you replace your call with

$.ajax({
    url: 'http://localhost/call',
    dataType: 'json',
    success: function(){},
    error: function(xhr, textStatus, errorThrown){
         //you should get a parse error and end up here
    }
});

you should end up in the error callback.

In your php file:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$arr = array('something' => 1, 'somethingelse' => 2);

echo json_encode($arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM