<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var http = new XMLHttpRequest();
var params = 'frashnum=&action=login&Frm_Logintoken="+results+"&Username=admin&Password=test';
var url = 'http://page/';
http.open('POST', url, true); //Send the proper header information along with the request http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onload = function () {
let str = (http.responseText);
alert(str)
let pattern =/\bgetObj\("Frm_Logintoken"\)\.value = "([^"]+)";/;
let results = console.log(str.match(pattern)[1]);
return results;
}
console.log(results);
http.send(params);
</script>
</body>
</html>
i am trying to return the results variable to use it in Frm_Logintoken from the params but it says results is not defined . where clearly it’s defined… so i tried to remove the equals sign before the
function and this part too http.onload = and it worked but the rest of code didn’t … so is there a way to fix this ?
The problem that you’re facing is that the onload function doesn’t execute until long after the script has finished running.
The best way to deal with that is normally to check if readyState is 4 (which means done), and that the status is 200 (which means OK), and you’ll then have a meaningful response. In most web browsers that can also be achieved using the onload event.
You can then pass the result to some other function code.
function doSomethingWith(response) {
// do something based on the response here
}
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.onreadystatechange = function (evt) {
if(xhr.readyState === 4 && xhr.status === 200) {
doSomethingWith(xhr.responseText);
}
};
xhr.send();
function doSomethingWith(response) {
// do something based on the response here
}
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.onload = function success(evt) {
doSomethingWith(xhr.responseText);
};
xhr.send();
readyState is 4 (which means done), and that the status is 200 (which means OK)
what will it do ?
i need it to make sure that it works correctly
like here is my full code
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const http = new XMLHttpRequest();
var response;
var test;
var passa;
const url = 'http://page/';
http.open('get', url, false);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function ()
{
if(http.readyState === 4)
{
if(http.status === 200 || http.status == 0)
{
let str = (http.responseText);
let pattern =/\bgetObj\("Frm_Logintoken"\)\.value = "([^"]+)";/;
let results = str.match(pattern)[1];
alert(results)
response = results;
// there request into one
const http1 = new XMLHttpRequest();
const url1 = 'http://page/';
http1.open('post', url1, false);
http1.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
let params1 = 'frashnum=&action=login&Frm_Logintoken='+response+'&Username=test&Password=test';
test = params1
http1.send(test);
alert(test)
const http2 = new XMLHttpRequest();
const url2 = 'http://page/test.php';
http2.open('get', url2, false);
http2.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http2.send();
let str1 = (http2.responseText);
let patterx = /Transfer_meaning\('testtest','([^']*)'\)/g;
patterx.exec(str1)[1];
let pass = patterx.exec(str1)[1];
passa = pass;
alert(passa);
}
}
}
http.send();
</script>
</body>
</html>
the problem here i made there requests into a one function ( cause using more than one http on load in a new functions needed a time out to work and i don’t want that )
so i can’t use http on load again i think …
let’s say the first Frm_Logintoken results was undefined or null i need it to do that request again and don’t move to the other request till it get non of those two (undefined or null) … and then i don’t know about the post request maybe it needs http on load too so it works probably … and the third request should have the same thing as the first one as it extracts a value too !!