“Origin null is not allowed by Access-Control-Allow-Origin” error for request made by application running from a file:// URL

后端 未结 17 1410
孤城傲影
孤城傲影 2020-11-21 04:42

I\'m developing a page that pulls images from Flickr and Panoramio via jQuery\'s AJAX support.

The Flickr side is working fine, but when I try to $.get(url, ca

17条回答
  •  梦毁少年i
    2020-11-21 05:21

    Not all servers support jsonp. It requires the server to set the callback function in it's results. I use this to get json responses from sites that return pure json but don't support jsonp:

    function AjaxFeed(){
    
        return $.ajax({
            url:            'http://somesite.com/somejsonfile.php',
            data:           {something: true},
            dataType:       'jsonp',
    
            /* Very important */
            contentType:    'application/json',
        });
    }
    
    function GetData() {
        AjaxFeed()
    
        /* Everything worked okay. Hooray */
        .done(function(data){
            return data;
        })
    
        /* Okay jQuery is stupid manually fix things */
        .fail(function(jqXHR) {
    
            /* Build HTML and update */
            var data = jQuery.parseJSON(jqXHR.responseText);
    
            return data;
        });
    }
    

提交回复
热议问题