Assemble a URL with jQuery
I have a base url,
var url = "/example/url/here"
and an object
var data = {"id": 1, "do": "some action"}
and I want to assemble a url with these to items in a similar fashion to how the $.get works, only I just want the开发者_StackOverflow中文版 url. Any ideas or do I have to loop through the object and assemble the url myself?
Try using $.param
:
url += '?' + $.param(data);
Try it here
$.param(data);
See: http://api.jquery.com/jQuery.param/
var str = url+'?'+$.param(data);
This returns /example/url/here?id=1&do=some+action
.
Just a note that there were big changes between jQuery 1.3 and 1.4 in this library. See the COMMENTS section of http://api.jquery.com/jQuery.param/ and http://benalman.com/news/2009/12/jquery-14-param-demystified/. I'm not taking sides, but ASP apps are probably expecting the 1.3 behavior and PHP/Ruby probably expect 1.4, and there is a switch in 1.4 to make it act like 1.3, though not enabled by default.
精彩评论