dermoritz dermoritz - 7 months ago 38
Javascript Question

How to redirect to same page with parameters given as json

On my site i am using jQuery's ajax this way:

$.ajax({
type: 'GET',
url: productSearch.searchUrl,
data: parameter,
dataType: 'json'
}


"data" contains all parameters.

On some state i want to redirect the user to the same page but with all paramters from "data" appended.

Example

data = {
"par1" = "value1",
"par2" = "value 2"
}


current url:
www.google.com

should be redirected to
www.google.com?par1=value1&par2=value2
.

How to do this (with jQuery?)?

Answer

try:

var data = {
    "par1": "value1",
    "par2": "value 2"
}
var params = $.param(data);
if (location.href.match(/\?/)) {
    location.href += params;
} else {
    location.href += '?' + params;
}