我在JS以下API:Nodejs和expressjs。阿贾克斯get请求不返回数据
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");});
});
发帖时使用招,让使用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,但没有数据返回,并调用失败功能
您的请求调用“/ API /书”,你只能听“/书籍” – nada
你有没有调试客户端和服务器的结果,以确保它有什么数据? –
İlkerKorkut:调试服务器将是一个任务,因为之前从未调试节点。纳达:事实并非如此。我打电话的网址是正确的 – user1809104