Using the named function as a callback for $ .getJSON in jQuery to satisfy Facebook signature requirements

I am trying to access the Admin.getMetrics API API through jQuery. I am correctly composing the request URL on the server side (in order to secretly store my application). Then I send the URL to the browser to request with jQuery.getJSON().

Facebook requires me to send a copy of all my request parameters hashed using my application, along with the request to verify my authenticity. The problem is that jQuery wants to generate the name of the callback function itself to match the name it gives to the anonymous function you pass in to call when returning data. Therefore, the function name is not available until it jQuery.getJSON()is executed, and Facebook considers my request invalid because of an inconsistent signature (the signature I send does not include the correct callback parameter, because it was not created before the jQuery.getJSON()RAN).

The only way I can solve this problem is to somehow indicate the name of my function on jQuery.getJSON()instead of letting it remain anonymous. But I can not find any option for this in jQuery AP.

+3
source share
4 answers

jQuery.getScript - - . getScript jQuery, ( , , ). jQuery.getScript, jQuery Ajax, _=12344567 ( 1234567 - ). jQuery , . , .

#jquery , jQuery - jQuery.Ajax :

jQuery.ajax({
  url: fbookUrl,
  dataType: "script",
  type: "GET",
  cache: true,
  callback: null,
  data: null
});

( fbookUrl - URL- API Facebook, , callback=myFunction). dataType: "script" , JSONP script , cache: true jQuery , .

+2

, ,

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

+3

JSONP $.ajaxSetup, , , :

jsonp
jsonp. " " " =?" URL- GET POST. , {jsonp: 'onJsonPLoad'} 'onJsonPLoad =?' .

. http://docs.jquery.com/Ajax/jQuery.ajax#options

+1

:

window.fixed_callback = function(data){
    alert(data.title);
};

$(function() {
    $.getScript("http://api.flickr.com/services/feeds/photos_public.gne?tags=cats&tagmode=any&format=json&jsoncallback=fixed_callback", function(data) {
    alert('done'); } );
});

The problem with this callback is that you can only process one type of request at a time, since the function is registered globally. The callback function would probably have to turn into a dispatcher for the various kinds of data that it could receive and call the corresponding function.

0
source

Source: https://habr.com/ru/post/1698287/


All Articles