Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery build http query string

Tags:

jquery

string

url

I have an object like this:

Object     id: "myid"     token: "sometoken" 

I need to build a HTTP query-string and get something like this:

http://domain.com/file.html?id=myid&token=sometoken 

Any ideas how I can do this?

like image 545
greenbandit Avatar asked Mar 15 '12 18:03

greenbandit


People also ask

How to get query string from URL in jQuery?

I need to get the value of location from the URL into a variable and then use it in jQuery code: var thequerystring = "getthequerystringhere" $('html,body'). animate({scrollTop: $("div#" + thequerystring).

How to get query params jQuery?

split('='); var key = params[0]; var value = params[1]; var obj = { [key] : value }; paramObj. push(obj); } console. log(paramObj); //Loop through paramObj to get all the params in query string.

What is the use of param () method in jQuery?

The param() method creates a serialized representation of an array or an object. The serialized values can be used in the URL query string when making an AJAX request.


2 Answers

​var obj = {         id    : 'myid',         token : 'sometoken'     };  alert($.param(obj)); 

You can use $.param() to create your query-string parameters. This will alert id=myid&token=sometoken.

This function is used internally to convert form element values into a serialized string representation.

Here is a demo: http://jsfiddle.net/RdGDD/

And docs: http://api.jquery.com/jquery.param

like image 189
Jasper Avatar answered Sep 23 '22 00:09

Jasper


var obj = { id: 'myid', token: 'sometoken' }; var url = 'http://domain.com/file.html?' + $.param(obj); 
like image 40
KL-7 Avatar answered Sep 23 '22 00:09

KL-7