-1

the code i am trying to run

<!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 ?

i have already tried a lot of the answers from other questions but non worked for me ... and i am too Beginner to understand not well Simplified answers.

so please don't mention links or mark it as duplicated and just answer it

mina nageh
  • 143
  • 2
  • 2
  • 13
  • That's a bit confusing. It looks like you want to use the API to retrieve the `results`, but in order to use the API, you need to have the `results` already (for the `params`)? Seems you're in a catch-22 – CertainPerformance Jun 11 '19 at 11:57
  • Possible duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – 04FS Jun 11 '19 at 12:09
  • @04FS what i don't understand is that is there no example that's the function followed by = – mina nageh Jun 11 '19 at 17:03
  • just an example is all i need . – mina nageh Jun 11 '19 at 17:04
  • https://www.webucator.com/how-to/how-use-the-callback-function-ajax.cfm explains how to use callback functions when you are dealing with XMLHttpRequest yourself. (As opposed to a framework like jQuery encapsulating that in an a bit less cumbersome interface, or using “modern” alternatives such as the Fetch API.) – 04FS Jun 12 '19 at 06:45
  • @04FS here is the new working code everyone i fixed it by var response and then response = results; and doing two requests one get and the other is post and adding the second one in set timeout function https://pastebin.com/mVnNDcAg what do you think that’s wrong in this code and can be improved ? … here what i think i need to do. make it redo the first request if the response is null or undefined and don’t do the second request till the response isn’t null or undefined. but i have no clue how to do this so any help is appreciated – mina nageh Jun 13 '19 at 17:25
  • @CertainPerformance ............ – mina nageh Jun 13 '19 at 17:25

2 Answers2

0

Learn the asynchronous behaviour of javascript. Your console.log(results) part is being called before the response of api call is received. Check How do I return the response from an asynchronous call?

Adams Hales
  • 119
  • 9
  • so can you fix the code in an answer ?. and a well Simplified examples will be better than these answers " How do I return the response from an asynchronous call?" is there a link outside of stack ? – mina nageh Jun 11 '19 at 17:08
0

an quick fix would be changing

http.open('POST', url, true);

to http.open('POST', url, false);

to make the request synchronous . and instead of return results;

add results1 = results;

and then

console.log(results1);

should work .. and if not add just add

var results1;

at the start of the code

so the full code should look like this

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script> 
var http = new XMLHttpRequest();
var url = 'http://page/';
var results1;
http.open('POST', url, false);
//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]);
    results1 = results;

 } 
 console.log(results1);
var params='frashnum=&action=login&Frm_Logintoken='+results1+'&Username=admin&Password=test';
http.send(params);
</script>
</body>
</html>

and the results in the params should be in Apostrophe not double quotes so it should look like this 'results1'

mina nageh
  • 143
  • 2
  • 2
  • 13