简体   繁体   English

如何在Ajax中调用Express route?

[英]How to call Express route in Ajax?

I'm having issues calling an Express route in my Node.js app from Ajax when a form is submitted. 提交表单时,我在从Ajax的Node.js应用程序中调用Express路由时遇到问题。

My route looks like this: 我的路线如下所示:

router.post("/user", function(req, res) {
    console.log("FORM DATA: " + JSON.stringify(req.body));
    res.send('done!');
});

My Ajax call looks like this: 我的Ajax呼叫看起来像这样:

$("#add-report-form").submit(function(e) {
  e.preventDefault();

  $.ajax({
     type: "POST",
     uri: "/user",
     contentType: "application/json",
     data: JSON.parse(JSON.stringify($("#add-report-form").serializeArray())),
     }).done(function(data) {
        alert("ajax call done!");
     });
  });

I have the following in my app.js app.use(bodyParser.json()); 我的app.js中有以下内容app.use(bodyParser.json()); .

When I submit my form, the console.log in my route does not run and I can't figure out why, it seems the route in is not getting called at all. 当我提交表单时,路由中的console.log无法运行,我也不知道为什么,似乎根本没有调用该路由。 Any help would be great! 任何帮助将是巨大的!

My form looks like this: 我的表格如下所示:

<div class="modal-body">
   <form id="add-report-form">
      <div class="row">
        <div class="col">
           <label for="report-select">Report</label>
           <select class="form-control" id="report-select" name="report">
             <option>Select Report</option>
           </select>
        </div>
       </div>

       <div class="row modal-row">
         <div class="col">
           <label for="interval-select">Report Interval</label>
             <select class="form-control" id="interval-select" name="interval">
               <option>Select Interval</option>
             </select>
          </div>

          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" id="add-submit" class="btn btn-primary save-changes">Save changes</button>
          </div>
      </form>
   </div>

EDIT: 编辑:

I also tried executing the Ajax on the click event of the button, but still no luck, the alert pops up but the route is still not called. 我也尝试过在按钮的click事件上执行Ajax,但还是没有运气,警报弹出了,但仍然没有调用路由。

$("#add-submit").on("click", function(e) {
    alert("this is the form submission");

    e.preventDefault();

    $.ajax({
        type: "POST",
        uri: "/user",
        contentType: "application/json",
        // data: JSON.parse(JSON.stringify($("#add-report-form").serializeArray())),
        data: JSON.stringify({data: "test"}),
    }).done(function(data) {
        alert("ajax call done!");
    });
});

The problem might be a typo. 问题可能是拼写错误。 Try 'url' instead of 'uri': 尝试使用“ url”而不是“ uri”:

$.ajax({
    type: "POST",
    url: "/user",
    contentType: "application/json",
    // data: JSON.parse(JSON.stringify($("#add-report-form").serializeArray())),
    data: JSON.stringify({data: "test"}),
}).done(function(data) {
    alert("ajax call done!");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM