views:

39

answers:

2

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?

A: 

try adding a callback on the url...

http://www.facebook.com/ajax/typeahead_friends.php?jsoncallback=?

Reigel
Nada. That wouldn't work anyway, would it? After all, `callback(for(;;);{'key': 'value'})` is not valid javascript.
Eric
I think there's no way around it... it's a restriction for same domain trick... read it here... http://stackoverflow.com/questions/856045/how-to-restrict-json-access
Reigel
A: 

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.

Sean Amos