简体   繁体   中英

Serializing a JSON object to send via AJAX

I'm trying to send some data in a JSobject via AJAX using jQuery.

Here's the json object.

var cookieData = {
        'land' : document.URL,
        'ref' : document.referrer
    };

The object is them stored in a cookie... via JSON.stringify(cookieData);

Before the form is sent and AJAX post is made, I need to get the object's properties:

var cookieData = cookie.get(website); //return the cookieData in stringified form.
var submitData = 'tracking=' +cookieData + jQuery("#quoteForm").serialize();

However, on the receiving application's end, the data is unusable, is there any way that I can serialize it for use in an AJAX post?

The receiving end is a PHP application, processed with:

$trackingData = json_decode(htmlspecialchars_decode($_POST['tracking']),true);

May be that the problem is a missing &, try this

var submitData = 'tracking=' + cookieData + '&' + jQuery("#quoteForm").serialize();

By the way, which error return php or the json_decode/whatever you use to decode the json?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM