0
$.get('api/dosomething.php',data,function(responseText){
    alert(responseText);
    var response = jQuery.parseJSON(responseText);
    alert(response);

最初のアラートは次のように述べています。Object (object)

ただし、次のアラートは実行されません。

Uncaught SyntaxError: Unexpected token ) file.php:1
4
Uncaught SyntaxError: Unexpected token o 

PHP:

 $result = array('id' => $db->lastInsertId());

    header('Content-Type: application/json');

    echo json_encode($result);
4

2 に答える 2

0

PHP スクリプトは、JSON を提供していることをブラウザーに伝えます ( Content-Type: application/json)。$.get はそれを自動的に検出し、フェッチされた JSON データを有効な JavaScript オブジェクトに変換します。

$.get('api/dosomething.php',data,function(data) {
    alert(data.id);
});


http://api.jquery.com/jQuery.ajax/#data-types から:

サーバーから返されることを期待しているデータのタイプ。何も指定されていない場合、jQuery は応答の MIME タイプに基づいて推測しようとします。

于 2013-08-10T15:31:41.657 に答える