簡體   English   中英

無法使用jquery發送簡單的ajax請求以獲取json文件

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

這是我的代碼:

$(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);
    }

});


});

無論我嘗試什么,都希望得到alert(2),但是當我檢查數據中的狀態時,我會看到成功。

該請求發送到nodejs服務器。 它作為響應發送一個json對象,如下所示:

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);
    });
});
};

json本身以[]開頭和結尾,因為它是一個數組。 我嘗試將其放入另一個json中,因此它看起來像是標准json:{a:items},但這也不起作用。

現在正在從另一台本地Web服務器進行ajax調用(我從本地html文件開始,但是移到Web服務器,因為盡管這可能是問題所在)。

還在節點中添加了allow-cross-origin頭。

我一無所知。

dataType:'jsonp'更改為dataType:'json'

看到這個問題的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM