Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js中的Ajax调用_Javascript_Jquery_Node.js_Ajax - Fatal编程技术网

Javascript node.js中的Ajax调用

Javascript node.js中的Ajax调用,javascript,jquery,node.js,ajax,Javascript,Jquery,Node.js,Ajax,大家好,我在使用ajax调用方面遇到了一个问题 我有两个ajax调用,所以第一个ajax将数据发送到服务器,服务器检查并返回值,我希望将返回的值发送给另一个ajax调用,但在第二个ajax调用中,我似乎没有传递任何内容 第一个Ajax调用 function first(){ $.ajax({ url:window.location + '/first', type: 'post', data : $('form#first_form').serialize() //

大家好,我在使用ajax调用方面遇到了一个问题

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

第一个Ajax调用

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(){

    }
  });
}
第二次Ajax调用

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(){

    }
  });
}
服务器

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

req.body.username
仅在使用body解析器中间件且请求是POST时有效
req.body
将是POST请求的解析正文

如果数据位于GET的查询字符串(例如,查询参数)中,并且它看起来像您的GET,则您需要:

req.query.username
你可以这样合并:

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");
});
这是我的建议


而且,您可以在中看到,
data
选项将内容放入查询字符串中。

是的,我在等待时间限制,它一直说我可以在几分钟后选择您的答案lol