Nodejs和expressjs。 Ajax获取请求不返回数据

时间:2015-04-21 12:30:23

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

我在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但没有返回数据并且调用失败函数

2 个答案:

答案 0 :(得分:2)

您的请求正在调用" / api / books"而且你只是在听#34; / 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");});
    });

答案 1 :(得分:0)

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

相关问题