JSONP call showing “Uncaught SyntaxError: Unexpected token : ”

后端 未结 3 1638
故里飘歌
故里飘歌 2020-11-29 04:08

Here is my code

$.ajax({
        url: \'https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e         


        
相关标签:
3条回答
  • 2020-11-29 04:23

    You're trying to access a JSON, not JSONP.

    Notice the difference between your source:

    https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=?

    And actual JSONP (a wrapping function):

    http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=processJSON&tags=monkey&tagmode=any&format=json

    Search for JSON + CORS/Cross-domain policy and you will find hundreds of SO threads on this very topic.

    0 讨论(0)
  • 2020-11-29 04:32

    Working fiddle:

    http://jsfiddle.net/repjt/

    $.ajax({
        url: 'https://api.flightstats.com/flex/schedules/rest/v1/jsonp/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59',
        dataType: 'JSONP',
        jsonpCallback: 'callback',
        type: 'GET',
        success: function (data) {
            console.log(data);
        }
    });
    

    I had to manually set the callback to callback, since that's all the remote service seems to support. I also changed the url to specify that I wanted jsonp.

    0 讨论(0)
  • 2020-11-29 04:37

    I run this

    var data = '{"rut" : "' + $('#cb_rut').val() + '" , "email" : "' + $('#email').val() + '" }';
    var data = JSON.parse(data);
    
    $.ajax({
        type: 'GET',
        url: 'linkserverApi',
        success: function(success) {
            console.log('Success!');
            console.log(success);
        },
        error: function() {
            console.log('Uh Oh!');
        },
        jsonp: 'jsonp'
    
    });
    

    And edit header in the response

    'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE'

    'Access-Control-Max-Age' , '3628800'

    'Access-Control-Allow-Origin', 'websiteresponseUrl'

    'Content-Type', 'text/javascript; charset=utf8'

    0 讨论(0)
提交回复
热议问题