0

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?

Eric
  • 95,302
  • 53
  • 242
  • 374

2 Answers2

0

try adding a callback on the url...

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

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Nada. That wouldn't work anyway, would it? After all, `callback(for(;;);{'key': 'value'})` is not valid javascript. – Eric Jul 01 '10 at 08:14
  • 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 Gallarde Jul 01 '10 at 08:59
0

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
  • 2,260
  • 19
  • 13