48

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=e0ea60854c1205af43fd7b1203005d59&callback=?',
        dataType: 'JSONP',
        jsonpCallback: 'jsonCallback',
        type : 'GET',
        async: false,
        crossDomain: true,
        success: function(data) {
            console.log(data);
        }
    });

What am I doing wrong? should I add or change anything in here? Any help would be appreciated. Thanks

4
  • please remove space before type : 'GET', Commented Oct 3, 2013 at 18:02
  • 2
    It's JSON, not JSONP on your url. Commented Oct 3, 2013 at 18:09
  • async: false doesn't work with JSONP. Why are you using jsonpCallback AND callback=? crossDomain is not needed for cross-domain requests (ironic isn't it?) Commented Oct 3, 2013 at 18:10
  • The API you are requesting from doesn't appear to support JSONP, that or you are using the API incorrectly for a JSONP response. Commented Oct 3, 2013 at 18:11

3 Answers 3

52

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I find it extremely ironic that your username is "Jason P". :)
do you have to define a function named callback somewhere?
@ConnorLeech No, jQuery creates the callback function for you.
I actually get the same error as the OP with $.ajax as I do with Angular's $http, in my attempt at jsonp
I am still getting the error... :/ jsfiddle.net/w8e296zj. I see it is only a JSON response hence the JSONP is giving the issue mentioned above. If I use JSON as a datatype, I get the CORS issue. Any idea how to fix it? Thanks.
|
18

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.

1 Comment

All i have to do is specify JSONP in the URL as supposed to JSON as shown currently in the URL. Thankfully, the third party supports JSONP based on the link
-1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.