Reputation: 871
How would i do the following in javascript?
Do a GET
call to a URL, with additional parameter .e.g.
I want to do GET
to http://test with parameter myid = 5.
Thanks, Boots
Upvotes: 4
Views: 22308
Reputation: 41533
If by "Do a 'GET' call to a url" you mean to change the current location to a certain url, all you have to do is assign the new url to the location
variable:
var newUrl = "http://test";
window.location = newUrl;
If you want to construct the url by adding some query parameters, you can do :
newUrl += "?myid=" + myid;
In addition, you can use a function to map the parameters to the url :
function generateUrl(url, params) {
var i = 0, key;
for (key in params) {
if (i === 0) {
url += "?";
} else {
url += "&";
}
url += key;
url += '=';
url += params[key];
i++;
}
return url;
}
And then you could use it as :
window.location = generateUrl("http://test",{
myid: 1,
otherParameter: "other param value"
});
obs: this function only works for int/bool/string variables in the params object. Objects and arrays cannot be used in this form.
Upvotes: 3
Reputation: 4063
If you only want to call it. And don't go to it: Use an AJAX request:
$.ajax({
url: 'http://test/?myid=5'
});
Here in jQuery. But there are enough non-jQuery examples on the internet.
Upvotes: 0
Reputation: 122916
try something like:
location.replace('http://test.com/sometest.html?myid=5&someotherid=6');
or
location.href = 'http://test.com/sometest.html?myid=5&someotherid=6';
Upvotes: 3
Reputation: 20982
You'd just include it normally in the query string: http://test?myid=5&otherarg=3
Upvotes: 0