node.js中的Ajax调用


                    

大家好,我在使用ajax调用方面遇到问题。</ p>

我有两个ajax调用,因此第一个ajax将数据发送到服务器,服务器检查并返回值,我希望将返回的值传递给另一个ajax调用,但似乎在第二个ajax调用中没有传递任何内容。</ >

第一次Ajax呼叫</ p>

  function first(){

   $ .ajax({
     url:window.location +'/ first',
     类型:“ post”,
     data:$('form#first_form')。serialize()//类似于“ name = Brad”,
     成功:功能(响应){
       consol.log(response.name); //我检查了一下,并返回“ Brad”

       second_ajax(response.name); //传递返回值“ Brad”

     },错误:function(){

     }
   });
}
</ code> </ pre>

第二个Ajax调用</ p>

 功能秒(响应名称){

   $ .ajax({
     url:window.location +'/ second',
     类型:“获取”,
     数据:{username:response_name} //我不确定如何在这里写,我想像'username = Brad'一样发送
     成功:功能(结果){
     },错误:function(){

     }
   });
}
</ code> </ pre>

服务器</ p>

  app.get('/ second',(req,res)=> {
           //从第二个ajax调用中获取用户名
           var user_name = req.body.username;} //这里我什么也没得到...
</ code> </ pre>
     </ div>

展开原文

原文

Hi guys I am having a issue in terms of using ajax call.

I have two ajax calls so first ajax sends the data to the server and server checks and return the value and I want the returned values to the another ajax call but it seems like I am passing nothing in second ajax call.

First Ajax Call

function first(){

  $.ajax({
    url:window.location + '/first',
    type: 'post',
    data : $('form#first_form').serialize() //it will be like "name=Brad",
    success: function(response){   
      consol.log(response.name); // I checked, and it returns "Brad"

      second_ajax(response.name); //pass the returned value "Brad"

    },error: function(){

    }
  });
}

Second Ajax call

function second(response_name){

  $.ajax({
    url:window.location + '/second',
    type: 'get',
    data : {username:response_name}//I am not sure how to write here, I want to send like 'username=Brad'
    success: function(result){ 
    },error: function(){

    }
  });
}

Server

 app.get('/second', (req, res) => {
          //get the username from the second ajax call
          var user_name = req.body.username;} //I am getting nothing here...

1个回答


req.body.username </ code>仅在您使用body-parser中间件且请求为POST时有效。 req.body </ code>将是POST请求的已解析主体。</ p>

如果数据在看起来像您的GET的查询字符串(例如查询参数)中,则您需要:</ p>

  req.query.username
</ code> </ pre>

您可以这样合并:</ p>

  app.get('/ second',(req,res)=> {
       //从第二个ajax调用中获取用户名
       var user_name = req.query.username;} //我在这里什么都没得到。
       console.log(用户名);
       res.send(“ get it”);
});
</ code> </ pre>

这是 req.query </ code>的快速文档 。</ p>

而且,您可以在用于 $。ajax()的jQuery文档中查看</ code> data </ code>选项将内容放入查询字符串。</ p>
     </ div>

展开原文

原文

req.body.username only works if you're using the body-parser middleware and the request was a POST. req.body would be the parsed body of the POST request.

If the data is in the query string (e.g. a query parameter) of a GET which it looks like yours is, then you want:

req.query.username

And you could incorporate that like this:

app.get('/second', (req, res) => {
      //get the username from the second ajax call
      var user_name = req.query.username;} //I am getting nothing here..
      console.log(user_name);
      res.send("got it");
});

Here's the Express doc for req.query.

And, you can see in the jQuery doc for $.ajax() that the data option puts things into the query string.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐