首页 > 解决方案 > 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 请求超时。这个你能帮我吗。

标签: node.jsajax

解决方案


你看到客户端超时了吗?

如果是这样,如果订单创建成功,我看不到 res.send(status_code, res_object) 或 res.json(res_object),必须发回响应并将请求标记为已完成。


推荐阅读