3

私は現在ローカルホストで実行しています:

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

http://localhost/callを返しますが{something:1}、何も警告されません。

4

1 に答える 1

8
{something:1}

ただし、有効なJSON文字列ではありません

{"something":1}

は。

通話を次のように置き換える場合

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

errorあなたはコールバックで終わるはずです。

あなたのphpファイルで:

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);
于 2013-02-05T22:43:11.743 に答える