节点js发布请求

时间:2018-09-28 22:16:21

标签: node.js xmlhttprequest

Success localhost response

Cannot GET /u/coupons at server

  

发布的前端脚本

<% data_body_bg_imgs.each do |img_hash| %>
  

服务器脚本

<script>
var count = document.getElementById("count");
var len = document.getElementById("length");
var pattern = document.getElementById("pattern");

document.getElementById('coupons-button').onclick = function()
{
    if(count.value!=="" && len.value!=="")
    {
        const genReq = new XMLHttpRequest();
        let url = `count=${encodeURI(count.value)}&` +
                  `len=${encodeURI(len.value)}&` +
                  `pattern=${encodeURI(pattern.value)}`;

        genReq.open('POST',`/u/coupons?${url}`,true);
        genReq.send();

        genReq.onreadystatechange = e => {
            if(genReq.readyState === 4 && genReq.status === 200){
                let gen = JSON.parse(genReq.response);
                if (gen.state === "SUCCESS")
                {                         
                    var coupons = gen.coupons;
                    for(var i=0;i<coupons.length;i++)
                    {   
                        var div = document.createElement('div');
                        var text = document.createTextNode(coupons[i]);
                        div.appendChild(text);
                        document.getElementById('coupons-check').appendChild(div);
                    }

                } else {
                    var div = document.createElement('div');
                    var text = document.createTextNode("FAIL TO GENERATE");
                    div.appendChild(text);
                    document.getElementById('coupons-check').appendChild(div);

                }
            }
        }
    }
}
  

CouponWorker

admin.post( '/u/coupons' ,(req,res)=>{
let params = getParameters(req);
  CouponWorker.generateCoupons({
      "len":decodeURI(params.len),
      "count":decodeURI(params.count),
      "pattern":decodeURI(params.pattern)
  }).then((_res) =>{
      console.log(_res)
      if(_res.success === true)
      {
          res.status(200).json({
              "state":"SUCCESS",
              "coupons":_res.coupons
          });
      }
      else{
          res.status(200).json({"state" : "FAILED"});
      }
  });
  });

以上给出的快照显示了在本地主机和服务器上运行的相同代码, 本地主机工作正常,提供输出并将数据保存到firbase,而服务器响应404找不到资源。 我找不到此错误的原因。我在邮递员上尝试了url req,响应与上面相同

0 个答案:

没有答案