Hate Names
Hate Names

Reputation: 1606

Receiving multiple variables from a XMLHttpRequest

I found out how to send multiple variables on SO using this:

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://127.0.0.1:3000?var1=" + name + "&var2=test", true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){

    }
}

The problem is, my node.js var queryObject = url.parse(req.url,false).query; turns into queryObject = 'var1=Robert&var2=test'. I was expecting var1='Robert'; var2='test'; Is there a way to do that with a command?

The only way I can think of doing it is by doing this:

xmlhttp.open("GET","http://127.0.0.1:3000? + name + "&test", true);

and then node.js 

var queryObject = url.parse(req.url,false).query;
var kk = queryObject.split("&");

but this way doesn't seem to work either for some reason. Is there a simple command that I'm missing?

Upvotes: 0

Views: 46

Answers (1)

Kris Erickson
Kris Erickson

Reputation: 33844

Do it the first way you had it:

xmlhttp.open("GET","http://127.0.0.1:3000?var1=test1&var2=test2", true);

and use queryString.parse to pull out the variables you want to be able to read.

var res = querystring.parse(req.url)

res will be

{ 
  var1: 'test1',
  var2: 'test2'
}

Upvotes: 1

Related Questions