node.js - Ajax 请求总是超时
问题描述
我正在制作一个食品订购网站,我必须将所有订单详细信息从 ejs 页面发送到节点后端。我的代码如下所示。我遇到的问题是,无论超时时间为 10 秒到一分钟(没有数据正在传输并且在给定时间超时),ajax 请求总是超时。
$.ajax({url:"/checkout",
timeout:10000,
type:"POST",
data:{
restro:b,
custo:a,
loc:c,
bill:bill,
order:arr,
slot:e,
pay:f,
room:g},
success:(result)=>{
console.log(result+" its here");
},
error:(x,y,error)=>{console.log(error)}});
在后端工作的nodejs代码是: -
app.post("/checkout",isLoggedIn,(req,res)=>{
for(var i=0;i<req.body.order.length;i++){
if(req.body.order[i].itname=="nullified")
req.body.order.splice(i,1);
i=0;
}
var orderpp={
restroname:req.body.restro,
cusname:req.body.custo,
deliveryadd:req.body.loc,
bill:req.body.bill,
items:req.body.order,
deliveryslot:req.body.slot,
payment:req.body.pay,
roomdet:req.body.room
};
console.log(orderpp.items);
Order.create(orderpp,(err,neworder)=>{
if (err) {
req.flash("error",err.message);
res.redirect("/index");
} else {
console.log(neworder);
}
})
})
预期输出是要在服务器控制台中创建和显示的新记录,但 ajax 请求超时。这个你能帮我吗。
解决方案
你看到客户端超时了吗?
如果是这样,如果订单创建成功,我看不到 res.send(status_code, res_object) 或 res.json(res_object),必须发回响应并将请求标记为已完成。
推荐阅读
- canvas - P5 画布宽度或高度偏移
- swift - 当用户按下“添加”按钮时,如何向我的 UITableView 添加下拉选择?
- ios - 在 Firebase 中存储下载的 Gif
- php - 从具有分隔字符串作为键的 JSON 中获取值
- mongodb - 如何获取名称与正则表达式模式匹配的集合列表并删除它们
- php - 问:如何在不使用 XPath 的情况下从网页中查找部分
- javascript - 字段自动归档时触发 OnChange?
- ruby-on-rails - Rails / ActiveAdmin - 如何将自定义数据从控制器传递到内容
- openlayers - 单击 OpenLayers 5 Typescript 将多个特征添加到矢量图层
- docker - 如何将 Docker Desktop Kubernetes 集群迁移到 Google Kubernetes Engine