How to request Facebook API using JSONP

Shouldn't I follow an AJAX request with jQuery work?

$.getJSON('https://graph.facebook.com/138654562862101/feed?callback=onLoadJSONP');

I defined a callback function with a name onLoadJSONP.

But Chrome gives me a typical Same-Origin-Policy error:

XMLHttpRequest cannot load https://graph.facebook.com/138654562862101/feed?callback=onLoadJSONP . The origin of null is not allowed by Access-Control-Allow-Origin.

I thought JSONP worked around this, what am I doing wrong?

+5
source share
3 answers

jQuery JSONP, callback=?, , , . :

$.getJSON('https://graph.facebook.com/138654562862101/feed?callback=?', onLoadJSONP);

callback=?, . , JSONP, XMLHttpRequest ... - .

+17

"callback =?" .

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "cat",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    $.each(data.items, function(i,item){
      $("<img/>").attr("src", item.media.m).appendTo("#images");
      if ( i == 3 ) return false;
    });
  });
+4

JavaScript, AJAX.

jQuery.support.cors = true;

$.ajaxTransport("+*", function( options, originalOptions, jqXHR ) {

  if(jQuery.browser.msie && window.XDomainRequest) {

    var xdr;

    return {

        send: function( headers, completeCallback ) {

            // Use Microsoft XDR
            xdr = new XDomainRequest();

            xdr.open("get", options.url);

            xdr.onload = function() {

                if(this.contentType.match(/\/xml/)){

                    var dom = new ActiveXObject("Microsoft.XMLDOM");
                    dom.async = false;
                    dom.loadXML(this.responseText);
                    completeCallback(200, "success", [dom]);

                }else{

                    completeCallback(200, "success", [this.responseText]);

                }

            };

            xdr.ontimeout = function(){
                completeCallback(408, "error", ["The request timed out."]);
            };

            xdr.onerror = function(){
                completeCallback(404, "error", ["The requested resource could not be found."]);
            };

            xdr.send();
      },
      abort: function() {
          if(xdr)xdr.abort();
      }
    };
  }
});
+1
source

All Articles