2015-09-10 52 views
2

我使用圖形API搜索的組,這裏是http請求:Javascript - 如何解析JSON圖中的PHP API從HTTP獲取?

var url ="https://graph.facebook.com/search?type=group&q=house&access_token=access_token&callback=FB.__globalCallbacks.f317bdb78&limit=5&method=get&pretty=0&sdk=joey". 

$.get(url, function(resp) { 
    console.log(resp);//it's return resp 
    var obj = jQuery.parseJSON(resp.data);//I got error here, resp.data is undefined 
    console.log(obj); 
}); 

這是從圖形API響應:

/**/ FB.__globalCallbacks.f317bdb78({"data":[{"name":"House Party !!!","privacy":"CLOSED","id":"881671771927203"},{"name":"KITTY'S HOUSE","privacy":"OPEN","id":"100126236854592"},{"name":"Full House Long Xuy\u00ean","privacy":"OPEN","id":"606749376035865"},{"name":"Housing Ho Chi Minh City (Saigon)","privacy":"CLOSED","id":"330339417123482"},{"name":"Ch\u0103n Drap House","privacy":"OPEN","id":"1397025403874006"}],"paging":{"cursors":{"before":"MAZDZD","after":"NAZDZD"},"next":"https:\/\/graph.facebook.com\/v2.0\/search?access_token=access_token&pretty=0&q=house&type=group&limit=5&after=NAZDZD"}}); 

如何從響應曲線圖上搜索Facebook的解析JSON數據?

回答

0

如果您正在使用JSONP(注意:callback=? GET參數),那麼你的代碼應該是:

var url ="https://graph.facebook.com/search?type=group&q=house&access_token=access_token&callback=?&limit=5&method=get&pretty=0&sdk=joey"; 

$.getJSON(url, function(resp) { 
    console.log(resp); 
}); 

或多個通用:

var url ="https://graph.facebook.com/search?type=group&q=house&access_token=access_token&callback=?&limit=5&method=get&pretty=0&sdk=joey"; 

$.ajax({ 
    url: url, 
    dataType: 'jsonp' 
}).then(function(resp) { 
    console.log(resp); 
}) 
0

爲什麼首先使用JSONP?

var url ="https://graph.facebook.com/search?type=group&q=house&access_token=access_token&limit=5". 

$.getJSON(url, function(resp) { 
    console.log(resp.data); 
}); 

將是完美的罰款恕我直言...