简体   繁体   English

使用来自XMLHttpRequest的响应

[英]Using Response from XMLHttpRequest

Here's the code I have right now: 这是我现在拥有的代码:

var xhttp = new XMLHttpRequest();
xhttp.open("POST", "response.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("op=login_new");

var xhttp2 = new XMLHttpRequest();
xhttp2.open("POST", "verify.php", true);
xhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp2.send("content=" + xhttp.response);

This line appears to be the problem: 这行似乎是问题所在:

xhttp2.send("content=" + xhttp.response);

Basically I'm wanting to take the response from the first XMLHttpRequest and send a POST request containing that response to the second one. 基本上,我想从第一个XMLHttpRequest获取响应,并将包含该响应的POST请求发送到第二个。

What am I doing wrong? 我究竟做错了什么?

XMLHttpRequest is asynchronous, you can do it like this: XMLHttpRequest是异步的,您可以这样做:

var xhttp = new XMLHttpRequest();

xhr.onload = function (response) {
  var xhttp2 = new XMLHttpRequest();
  xhttp2.open("POST", "verify.php", true);
  xhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp2.send("content=" + response);
};

xhttp.open("POST", "response.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("op=login_new");

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

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