【问题标题】:Ajax synchronous post method, get data from Node server sideAjax同步post方法,从Node服务器端获取数据
【发布时间】:2018-11-04 15:59:01
【问题描述】:

我正在开发一个应用程序,在 window.unload 事件中,我正在对节点服务器端进行同步 ajax POST 调用。这是代码:

$(window).on('unload', function() {
              console.log("in Unload");

              if(!submittedFlag) {
                    var formData = $('#testsForm').serializeArray();
                    console.log(formData);
                    $.ajax({
                          type: 'POST',
                          data: formData,
                          async: false,
                          url: "/tests/ajaxSubmit",
                          success: function () {
                                console.log(msg);
                          }
                     })
                  }
});

服务端有post方法:

router.post('/tests/ajaxSubmit',async (req,res) => {
//my computation here
res.end();
});

如何将 POST 方法中计算的响应发送到前端 HTML 端。请帮帮我。

【问题讨论】:

    标签: javascript node.js ajax express


    【解决方案1】:

    使用 res.json 方法向客户端发送响应。

    router.post('/tests/ajaxSubmit',async (req,res) => {
      //my computation here
      return res.json({
        message: 'Done'
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-27
      • 2020-09-26
      • 1970-01-01
      • 2017-09-11
      • 2013-05-24
      • 2023-03-17
      • 2022-12-04
      • 1970-01-01
      相关资源
      最近更新 更多