[英]JSON response returns undefined
首先使用'json_decode'將數組轉換為JSON響應
$test = [ "connections" => [ [ "title" => "Connection 1", "date" => "01-26-2010", "id" => "1" ] ] ];
echo json_encode( $test );
然后在前端處理JSON響應。
$.get( 'http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e){
console.log( e.connections );
});
但不幸的是,它返回“ undefined”
使用JSON編輯器的響應視圖
我可以做這個
$.get( 'http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e){
$.each(JSON.parse(e),function(i,e){
$.each(e,function(i,e){
console.log(e.title);
});
});
});
它肯定會返回我想要的數據,但我不希望進行第二次循環。
任何想法,請幫忙嗎?
嘗試這個。 如果我沒記錯的話,我確定您的PHP
沒有header('Content-type:application/json')
。 如果沒有,它將作為string
返回。 您需要使用jQuery
內置method
$.parseJSON
進行解析
$.get('http://cnbusiness.nextdayhost.com/ajax/get_business_connections', function(e) {
$.each($.parseJSON(e),function(i,e){
// code here ...
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.