Try the following function. It works for me. JSON compatible.
function gotoUrl(path, params, method) {
method = method || "post";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
if (typeof params === 'string') {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", 'data');
hiddenField.setAttribute("value", params);
form.appendChild(hiddenField);
}
else {
for (var key in params) {
if (params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
if(typeof params[key] === 'object'){
hiddenField.setAttribute("value", JSON.stringify(params[key]));
}
else{
hiddenField.setAttribute("value", params[key]);
}
form.appendChild(hiddenField);
}
}
}
document.body.appendChild(form);
form.submit();
}
Demo version
<script>
$('#GotoNextPage').on('submit', function(evt){
evt.preventDefault();
var sampleData = new Object();
sampleData.currentPage = 'We are here';
sampleData.currentUid = 5;
gotoUrl("actionPage.php", {'cmd':'nextPage', 'data':sampleData});
});
</script>
Hope this helps!
source
share