0

I have the following code which sends post data to my form and alerts the expected result, however I'm finding it hard to save this as a variable I can use outside the function, can anyone offer any help?

<script>
var http = new XMLHttpRequest();
var url = "form.php";
var params = "name=test";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.(params);
</script>
Hugh
  • 1

1 Answers1

0

Just declare a variable and set it when the request responsed by server;

var http = new XMLHttpRequest();
var url = "form.php";
var params = "name=test";
var response;
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
        response = http.responseText;
    }
}
http.(params);
lucky
  • 12,734
  • 4
  • 24
  • 46