Is it possible to send object in url jquery

15,650

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

jQuery.param():

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.

Share:
15,650
Pekka
Author by

Pekka

Updated on June 27, 2022

Comments

  • Pekka
    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
    Pekka about 9 years
    it 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
    Pekka about 9 years
    how can i retrieve these value in the second page?
  • Pekka
    Pekka about 9 years
    what i am getting in url is ?data=undefined=
  • Pekka
    Pekka about 9 years
    my question is how will i get the value now in second page?
  • marsh
    marsh about 9 years
    there is simple function for parsing get params by JS stackoverflow.com/questions/901115/…
  • Pekka
    Pekka about 9 years
    should i pass the entire url to the function of just the data?
  • marsh
    marsh about 9 years
    for get a data param you call like this: getParameterByName('data')
  • Taz
    Taz about 9 years
    @Pekka Updated my answer