Is it possible to send object in url jquery
Solution 1
if you definetly want to use GET
request, use JSON.stringify
:
var url = '../reports/student.php?data='+ encodeURIComponent(JSON.stringify(data));
But better way will be a POST
request.
You also can use jQuery.param()
:
var data = {
name: 'yourname',
age: 1,
}
var params = jQuery.param(data) // 'name=yourname&age=1'
var url = '../reports/student.php?'+ params;
Solution 2
Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request. In case a jQuery object is passed, it should contain input elements with name/value properties.
The doc also states, that
If the object passed is in an Array, it must be an array of objects in the format returned by
.serializeArray()
So in your case:
var url = '../reports/student.php?' + jQuery.param(serializeArray(data));
To unserialize you should use jQuery BBQ's deparam function, as per cce's answer in this SO question: The $.param( ) inverse function in JavaScript / jQuery.
Pekka
Updated on June 27, 2022Comments
-
Pekka almost 2 years
data.push({ name: name, age: age, address: address, contactnumber: contactnumber }); var url = '../reports/student.php?data='+data; window.open(url, '_blank');
../reports/student.php?data=[object%20Object]
Above is an object i want to send to another page so i can retrieve it and use it on the next page to generate a report. The url is looking like the one above i am not sure if this is possible and if possible how can i get the value of each data in the object.If not possible please point me to the right direction any idea is ap
-
Pekka about 9 yearsit is ok if i dont stringify the data before sending i mean i want to do all retrieving in the next page?i mean i dont want to put stingify in the sending of data in url is it possible and how can i get the data now that i have sent it using jquery not php
-
Pekka about 9 yearshow can i retrieve these value in the second page?
-
Pekka about 9 yearswhat i am getting in url is
?data=undefined=
-
Pekka about 9 yearsmy question is how will i get the value now in second page?
-
marsh about 9 yearsthere is simple function for parsing
get
params by JS stackoverflow.com/questions/901115/… -
Pekka about 9 yearsshould i pass the entire url to the function of just the
data
? -
marsh about 9 yearsfor get a
data
param you call like this:getParameterByName('data')
-
Taz about 9 years@Pekka Updated my answer