1
var data = new FormData();
data.append("name", nickname.value);
data.append("email", email.value);
data.append("user", user.value);
data.append("passw", passw.value);

var req = new XMLHttpRequest();
req.onreadystatechange = function() { 
    if (req.readyState == 4 && req.status == 200)
        afterSignup(req.responseText);
}
req.open("GET", server, true); // true for asynchronous 
req.send(data);

function afterSignup(req) {
    res = JSON.parse(req)["res"];
    alert(res);
}

im trying to handle the requests asynchronised but im just not sure how to do it, this is what i have so far

  • Does this answer your question? [HTTP GET request in JavaScript?](https://stackoverflow.com/questions/247483/http-get-request-in-javascript) – Keith Mar 13 '20 at 16:39
  • "why am i getting empty responses" — What makes you think that you are? Nothing in the code you've shared is doing anything with the response that would display it anywhere. – Quentin Mar 13 '20 at 16:59
  • @Quentin i editied the question and forgot to switch the title, before i was doing it synchronised and errored as i put true in the param but now im asking how to do it asynchrous – Rafi Asmoucha Mar 13 '20 at 17:01
  • And what does "isnt working" mean? What do you expect to happen? How does this differ from what actually happens? – Quentin Mar 13 '20 at 17:02
  • @Quentin when it calls aftersignup it should json parse it and alert but doesnt alert which means it probably never gets called – Rafi Asmoucha Mar 13 '20 at 17:04
  • "it should json parse it and alert but doesn't alert" —Why should it do that? Hint **show us** the function you are calling. Add a debug statement at the top to see if it gets called. And if it doesn't, then figure out why. You only call it when a condition is met. *log the values that condition depends on* to see what they are. Look at the console to see if there are other error messages. – Quentin Mar 13 '20 at 17:05
  • Re edit: That's a start, now please response to the bits of my last comment that aren't just the first sentence. – Quentin Mar 14 '20 at 08:50

2 Answers2

0

Change async value to false to make it synchronous.

req.open("POST", `${server}/signup`, false);
Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28
  • im such an idiot... cant believe i spent the last hour stuck on this. thanks for the help bro it works, but what does that param actually mean or do – Rafi Asmoucha Mar 13 '20 at 16:35
  • 1
    No, don't do that.. It's a deprecated feature, so unless the OP is using a webworker it's going to break in the future. – Keith Mar 13 '20 at 16:36
  • @Keith so what should i use instead – Rafi Asmoucha Mar 13 '20 at 16:39
  • @RafiAsmoucha Check out the dupe I've just posted. – Keith Mar 13 '20 at 16:39
  • @Keith i barely used stackoverflow so how do i check that – Rafi Asmoucha Mar 13 '20 at 16:41
  • @RafiAsmoucha Use this link - https://stackoverflow.com/questions/247483/http-get-request-in-javascript – Jasdeep Singh Mar 13 '20 at 16:42
  • @RafiAsmoucha If you read the comments under your question, it should say something like -> `Does this answer your question?` with a link you can click on. If other users also flag this link your message will get marked duplicate too. – Keith Mar 13 '20 at 16:42
  • @Keith i tried to do it asynchrous but i couldnt get it to work, im very bad with javascript and this is th efirst time ive dealt with requests at all – Rafi Asmoucha Mar 13 '20 at 16:48
-1

There are so many JavaScript libraries out there ... big and small ... that I'd be hard pressed to suggest that you should spend much time re-inventing this.

For example: https://github.com/freelancephp/simpleAjax.js

 simpleAjax.get('ajax.html', function (data) {
     alert(data);
 });

Boom. Done. Over and out. The author says his library is "cross-platform," "even works for IE7+" (yuck), and is about 3KB minified. So, "just grab it and go."

(There are hundreds of packages out there like this one ... AJAX has truly been done to death.)

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41