Mattias
Mattias

Reputation: 3169

Howcome this ajax http request not working?

I'm using ajax to loop thru values and this one under is not working

$.ajax({
    type: 'GET',
    url: 'http://localhost:66514/test',
    contentType: 'application/json',
    success: function(data) {
        $.each(data, function(key, value) {});
    }
});

This one under is working just fine but I'd like the one above to work

$.getJSON('http://localhost:66514/test', function(data) {
    $.each(data, function(key, value) {});
});

So question is, whats wrong with my first ajax call? any help or input highly appreciated, thanks!

Upvotes: 0

Views: 160

Answers (2)

Jai
Jai

Reputation: 74738

The difference between these two are:

$.getJson() works because returned data is json and you get it in the success callback.

$.ajax() on the other hand you have to set the dataType:'json' to get the data.


Also if you are working with $.ajax() then you should use contentType:"application/json" when you are trying to send any data to the server.

Upvotes: 2

Mr. Go
Mr. Go

Reputation: 577

have you write the right code!!!

because in your code u made a mistake of

sucess: 

instead of

success:

Upvotes: 1

Related Questions