【问题标题】:cant send simple ajax request with jquery to get a json file无法使用 jquery 发送简单的 ajax 请求来获取 json 文件
【发布时间】:2013-09-05 11:03:35
【问题描述】:

这是我的代码:

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

});


});

无论我尝试什么,我都会收到警报 (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 服务器,因为我认为这可能是问题所在)。

还在节点中添加了允许跨域标头。

我一无所知。

【问题讨论】:

    标签: javascript jquery ajax json node.js


    【解决方案1】:

    dataType:'jsonp' 更改为dataType:'json'

    查看this question的答案

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 1970-01-01
      • 2013-05-12
      • 2014-10-24
      • 1970-01-01
      • 2014-11-11
      • 2014-05-07
      • 2020-10-04
      • 2014-05-18
      相关资源
      最近更新 更多