Javascript – using a named function as the callback for $.getJSON in jQuery to satisfy Facebook request signing demands

facebookjavascriptjqueryjsonjsonp

I'm trying to access the Facebook API Admin.getMetrics method via jQuery. I'm correctly composing the request url on the server side (in order to keep my app secret secret). I'm then sending the url over to the browser to be request using jQuery.getJSON().

Facebook requires that I send a copy of all of my request params hashed with my application secret along with the request in order to verify my authenticity. The problem is that jQuery wants to generate the name of the callback function itself in order to match the name it gives to the anonymous function you pass in to be called when the data returns. Therefore, the name of the function is not available until jQuery.getJSON() executes and Facebook considers my request to be inauthentic due to a mismatched signature (the signature I send along does not include the correct callback param because that was not generated until jQuery.getJSON() ran).

The only way I can think of out of this problem is to somehow specify the name of my function to jQuery.getJSON() instead of allowing it to remain anonymous. But I cannot find any option for doing so in the jQuery AP.

Best Solution

The only thing that did the work for me were the following settings

jQuery.ajax({ url: fbookUrl, dataType: "jsonp", type: "GET", cache: true, jsonp: false, jsonpCallback: "MyFunctionName" //insert here your function name });