2017-02-09 7 views
1

何とか私のjsonオブジェクトへのアクセス方法をajax呼び出しから見つけることができません。その後、私はデータを使ってアクセスしたいajax成功のJSONオブジェクトへのアクセス

echo json_encode(array(
    "nextId" => 2 
)); 

:ハンドラで は、私は次のIDをエコーてる

$.ajax({ 
    [...], 
    dataType: 'json', 
    success:function(data) { 
     console.log(data) 
     console.log(data['responseText']; 
     console.log(data['responseText'].nextId);  
     console.log(data['responseText']['nextId']); 
    } 
}); 

Result: Object {readyState: 4, responseText: "{"nextId":2}", responseJSON: Object, status: 200, statusText: "OK"} 
Result: {"nextId":2} 
Result: Undefined  
Result: Undefined 

私が見てNEXTID

+0

ログでわかるように、 'data.responseText'の値は**文字列**です。 jQueryを使用していますか? jQueryは自動的に応答をデコードする必要があります。あなたが成功関数の第一引数としてその価値を得ていることは奇妙に思えます。 –

+1

私はあなたが 'console.log(data ['responseJSON']。nextId);'にもっと運があると思います。 – MacPrawn

+0

@MacPrawnがうまくいきました。答えを提出する。 –

答えて

2

の値を取得したいあなたデータオブジェクト、responseJSONというオブジェクトが表示されます...代わりにこれを試してください:

$.ajax({ 
    [...], 
    dataType: 'json', 
    success:function(data) { 
     console.log(data) 
     console.log(data['responseJSON']; 
     console.log(data['responseJSON'].nextId);  
     console.log(data['responseJSON']['nextId']); 
    } 
}); 

希望する助けて!

関連する問題