【问题标题】:NodeJS How to get Data in server, sent from jquery ajax call via POSTNodeJS如何在服务器中获取数据,通过POST从jquery ajax调用发送
【发布时间】:2014-11-05 02:28:40
【问题描述】:

我的客户正在进行 ajax 调用

{{


        function callNode(){

        console.log("I am called");
        var data = {"emailId":"gopal@gmail.com"};



        $.ajax({
            type: 'POST',
            data: JSON.stringify(data),
           /* data: {
                blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]}
            },*/
            contentType: "application/javascript",
            //contentType: "application/x-www-form-urlencoded",
            dataType:'json',
            url: 'http://localhost:3000/notification',                      
            success: function(data) {
                console.log('success');
                console.log(JSON.stringify(data));                               
            },
            error: function(error) {
                console.log("some error in fetching the notifications");
             }

        });
    }
}}

我能够在我的 app.js 中获取此请求,但无法获取我正在传递的数据 我试图搜索,但没有任何工作

{{

      app.post('/notification', function(req, res) {
      JSON.stringify(req.params);


      /*
       var body;
       req.on('data', function(chunk) {
        console.log("Received body data:");       
         body += chunk;
      });

       // the end event tells you that you have entire body
      /*req.on('end', function () {
       try {
      var data = JSON.parse(body);
       colnosole.log(data);

    } catch (er) {
      // uh oh!  bad json!
      res.statusCode = 400;
      return res.end('error: ' + er.message);
    }

  }
   */


}}

事情是它没有进入任何请求和响应事件(因为我看到很多人使用它来获取数据。

帮助我知道这是第一次在节点上的 ajax 出了什么问题

【问题讨论】:

    标签: jquery ajax node.js post


    【解决方案1】:

    req.params 并没有按照你的想法去做。

    app.get('/:route', function(req, res) {
        console.log(req.params.route);
    });
    

    访问/test 将使用test 填充req.params.route

    您正在寻找req.body,它只能与body-parser 中间件一起使用。

    【讨论】:

      【解决方案2】:

      由于您将数据作为 json 发送,因此需要更改 contentType,因此 ajax 调用应该是:

      $.ajax({
              type: 'POST',
              data: JSON.stringify(data),
             /* data: {
                  blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]}
              },*/
              contentType: "application/json",
              //contentType: "application/x-www-form-urlencoded",
              dataType:'json',
              url: 'http://localhost:3000/notification',                      
              success: function(data) {
                  console.log('success');
                  console.log(JSON.stringify(data));                               
              },
              error: function(error) {
                  console.log("some error in fetching the notifications");
               }
      
          });
      

      在这里你可以看到contentType变成了application/json。

      在服务器端,您需要检查 request.body 以获取数据,而不是 request.params。

      【讨论】:

      • 它工作了,谢谢 V31。仅需要您提到的更改才能使其运行:)
      【解决方案3】:

      你必须使用body-parser中间件(https://www.npmjs.org/package/body-parser)并且你需要声明使用哪个badyParser

      • app.use(bodyParser.json()) 或

      • app.use(bodyParser.raw()) 或

      • app.use(bodyParser.text()) 或

      • app.use(bodyParser.urlencoded())

        在您的主 js 文件中。然后 req.body 对象将包含您的 json/text/raw/urlencoded 数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2012-03-06
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多