我正在使用图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数据?
最佳答案
如果您使用的是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);
})