juliet
juliet

Reputation: 897

Ajax request failing?

I am trying to retrieve json data from an api. It keeps failing, but when I look in the Net tab of Firebug I can see that the GET request executed and returned the correct data. Am I doing something wrong or does anyone have tips on how to debug this?

Edit: Have changed to dataType json and error status code is 0

Thanks

    $.ajax({
        url: 'http://localhost:55894/api/Test/All',
        data: {
            format: 'json'
        },
        error: function () {
            alert('Error');
        },
        dataType: 'jsonp',
        success: function (data) {
            alert('Ok');
        },
        type: 'GET'
    });

Upvotes: 0

Views: 83

Answers (3)

Chase
Chase

Reputation: 9362

From the info you provided the reason it is failing is because you dont have a cross domain access policy setup. Because you are using different ports to host the website and the API you are running into this issue. You can either setup a crossdomain.xml with the proper security settings or you can move both the API and the webserver to the same port.

Have a look at this for more info: http://en.wikipedia.org/wiki/Same-origin_policy

Upvotes: 1

qtuan
qtuan

Reputation: 687

JSON and JSONP are different. If you are using JSONP, the server side must be prepared to support it. Doesn't look like you are using JSONP.

So just change dataType to 'json', as you are "trying to retrieve json data".

Upvotes: 0

quang dung
quang dung

Reputation: 73

u can try this way:

$.ajax({
    type: 'GET',
    url: 'url api here',
    beforeSend: function() {
    },
    success: function(data) {

    },
    error: function(xhr) { // if error occured

    },
    complete: function() {

    },
    dataType: 'json'
});

Upvotes: 0

Related Questions