I want to use ajax to retrieve some JSON from another page, but I want to pass along the same GET params that were used to request the original page. How do I do tha开发者_C百科t? Does JS store them in a dict somewhere? Or is there a jQuery solution?
$.ajax({
url: 'mysecretwebpage.com/supersecret',
data: ???
});
The data you need you'll find in
window.location.search
Remove the first char from this string(will be the question mark, if GET is not empty)
I got this handy function:
document.getParameterByName = function (name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
};
Use it like:
var paramValue = document.getParameterByName('paramName');
turns out this has been asked before
精彩评论