How can I remove some useless text in a json request with jQuery?
I am doing a json request on what appears to be a Facebook API page.开发者_如何学编程 However, the json is prefaced with for (;;);
which is ruining jQuery's attempt to process it.
How can I slice off these unwanted characters? Can I use the dataFilter
property of the $.ajax
call?
My test code is:
$.ajax({
url: 'http://www.facebook.com/ajax/typeahead_friends.php',
data: {u: userid, __a: 1},
callback: function(data, status) {
alert(data);
//alert(data.payload.friends);
},
dataFilter: function(data,type) {
alert(data);
return data;
},
dataType: 'json'
});
However, the dataFilter
function is being given an empty string. Am I doing something wrong?
You can use dataFilter, I've used it before to process unwanted characters ASP.Net was inserting into JSON responses. For your case this should work:
$.ajaxSetup({
dataFilter: function(data, type) {
if (type === 'json') {
data.replace('for (;;);', '');
return JSON.parse(data);
}
return data;
}
});
You can use $.ajaxSetup to set the dataFilter globally so you don't have to set a dataFilter for every request.
try adding a callback on the url...
http://www.facebook.com/ajax/typeahead_friends.php?jsoncallback=?
精彩评论