Reputation: 47
On the web app I've been working, I've been using xmlhttprequests to pass single parameters to Java servlets as follows:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'DCC?command=' + encodeURIComponent(command), true);
xhr.send(null);
The problem is, I still don't really understand the syntax and now I need to do something similar with multiple parameters. How is this done?
Upvotes: 2
Views: 6429
Reputation: 1359
You could send the parameters in the send call.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'DCC', true);
xhr.send(JSON.stringify(parameters));
in the java end use the InputStreamReader
to read the request and deserialize the object.
Upvotes: 0
Reputation: 2379
The URL you're passing in the example above is:
'DCC?command=' + encodeURIComponent(command)
The DCC
part is actually part of the path to the webpage. It's short because it's a relative path. The fully qualified path would look something like www.sitename.com/DCC
The part after that (after the ?
character) is called the query string. That's the part of the URL that contains data that you're passing to the server (in a GET transaction), and it follows this pattern:
a=somevalue&b=anothervalue&c=yetanother
So add "&varnameA=valueA" to that string to pass both command
and varnameA
:
xhr.open('GET', 'DCC?command=' + encodeURIComponent(command)+"&varnameA=valueA",true);
You can keep tacking on &varname=value strings until your query is around 2000 characters, because that's where browsers commonly start crapping out because the URL is too long.
Remember to encode any special characters in the values (that's what encodeURIComponent()
is being used for) or you'll get some weird behavior. That means that you're appending something like +"&varnameA="+encodeURIComponent("valueA")
for each additional variable/value pair you want to pass to the server.
Upvotes: 1
Reputation: 433
Simple answer:
var url="index.php"+"?command="+cmd;
xhr.open("GET",url,true);
You can add more parameters, you only have to add a ? infront of each variable name.
Upvotes: 0
Reputation: 42
Syntax is : xhr.open("GET", url, true); in Url you can pass multitple parameter by appending "&"
Upvotes: 0