【问题标题】:Nodejs and expressjs. Ajax get request not returning dataNodejs 和 expressjs。 Ajax获取请求不返回数据
【发布时间】:2015-06-28 14:51:22
【问题描述】:

我在 JS 中有以下 Api:

router.route('/books')
    .get(function(req,res){
         Repository.getAll().done( function(result){ res.json(result);},function(err){res.send(err);}); 
    })
    .post(function(req,res){
         Repository.save(req.body).done( function(object){ res.json(object); },function(err){res.send("error seems to have occured");});
    });

当我使用 Fiddler 发布并使用 chrome 浏览器时效果很好。但是当我尝试使用 jquery 获取和发布时:

$.ajax({
                                type: "POST", 
                                url: "http://localhost:8000/api/books",
                                data: { "title":"My name is","releaseYear":"1989","director":"me","genre":"horror" }
                            }).done(function(data) {
                            //alert("Success.");
                            console.log("success");
                        }).error(function(data, err, e, o) {

                            console.log("error");
                            //alert("Sorry. Server unavailable. ");
                        });

                            $.ajax({
                                type: "GET", 
                                url: "http://localhost:8000/api/books",
                                contentType: "application/json"
                            }).done(function(data) {
                            //alert("Success.");
                            console.log("success");
                        }).error(function(data, err, e, o) {

                            console.log("error");
                            //alert("Sorry. Server unavailable. ");
                        });

失败/错误回调被触发。我已尝试指定 contentType 或将其删除,但无济于事。

调用POST时状态码为200但没有返回数据并调用fail函数

【问题讨论】:

  • 您的请求正在调用“/api/books”,而您只收听“/books”
  • 您是否调试过客户端和服务器端的结果以确保它有任何数据?
  • İlker Korkut:调试服务器将是一项任务,因为我以前从未调试过节点。纳达:事实并非如此。我打电话的网址是正确的
  • @user1809104 安装节点检查器以调试您的应用程序。这里是github.com/node-inspector/node-inspector

标签: jquery node.js express asp.net-web-api


【解决方案1】:

您的请求正在调用“/api/books”,而您只收听“/books”

将您的客户端代码请求网址修改为:

$.ajax({
                                type: "POST", 
                                url: "http://localhost:8000/books",
                                data: { "title":"My name is","releaseYear":"1989","director":"me","genre":"horror" }
                            }).done(function(data) {
                            //alert("Success.");
                            console.log("success");
                        }).error(function(data, err, e, o) {

                            console.log("error");
                            //alert("Sorry. Server unavailable. ");
                        });

                            $.ajax({
                                type: "GET", 
                                url: "http://localhost:8000/books",
                                contentType: "application/json"
                            }).done(function(data) {
                            //alert("Success.");
                            console.log("success");
                        }).error(function(data, err, e, o) {

                            console.log("error");
                            //alert("Sorry. Server unavailable. ");
                        });

或者修改你的服务器代码:

router.route('/api/books')
    .get(function(req,res){
         Repository.getAll().done( function(result){ res.json(result);},function(err){res.send(err);}); 
    })
    .post(function(req,res){
         Repository.save(req.body).done( function(object){ res.json(object); },function(err){res.send("error seems to have occured");});
    });

【讨论】:

  • 可能他正在使用前缀路由器app.use('/api', router);
  • 所以也许最好在他的问题中指定这一点:D
  • 正确的调用网址是:localhost:8000/api/books。我正在使用前缀路由器。如前所述,这似乎在其他任何地方都有效,但使用 jquery 调用时
  • @user1809104 在您的服务器上执行console.log(json(result));console.log(json(object)); 以查看您的服务器上的结果是否为空。从您的 ajax URL 中删除“localhost:8000”。检查 COR 的控制台错误或类似的东西。只需检查请求是否到达服务器以及服务器响应是什么(在服务器端)
  • Nada: using http.get('localhost:8000/api/books', function (http_res) { } 我能够从服务器端调用它获得结果。我也可以从 chrome 发出请求而没有问题. 简单返回的错误在失败回调中显示“错误”。不需要控制台记录任何内容,因为它可以在其他地方工作
【解决方案2】:

您是否尝试过添加dataType: "json"?您还应该在发布之前尝试在您的对象上运行JSON.stringify()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2021-03-25
    • 1970-01-01
    相关资源
    最近更新 更多