简体   繁体   中英

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.

What am I doing wrong?

XMLHttpRequest is asynchronous, you can do it like this:

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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