AAaa
AAaa

Reputation: 3819

cant send simple ajax request with jquery to get a json file

this is my code:

$(function() {

   $.ajax({
    url:'http://localhost:3000/business_places',
    type:'GET',
    dataType:'jsonp',
    done: function(data){
        alert(1)

    },
    error: function(data) {
        alert(2)
    },
    success: function(data) {
        alert(3);
    }

});


});

No matter what I try I leep getting the alert(2), but when I check the status in data I see success.

The request is to a nodejs server. It sends as a response a json object like this:

exports.getBusinessPlaces = function(req, res) {
console.log('Retrieving business places: ');

db.collection(BUSINESS_PLACE_COLLECTION, function(err, collection) {
    collection.find().toArray(function(err, items) {
        // var j = {a:items};
        res.send(items);
    });
});
};

The json itself starts and ends with [] because its an array. I tried putting in inside another json so it would look like a standards json : {a:items} but this didn't work either.

The ajax call is now being made from another local web server (I started with a local html file but moved to a web server because I though this might be the problem).

Also added in node a allow-cross-origin header.

I'm clueless.

Upvotes: 0

Views: 1468

Answers (1)

johnnynotsolucky
johnnynotsolucky

Reputation: 540

Change dataType:'jsonp' to dataType:'json'

See the answer to this question

Upvotes: 1

Related Questions