I’m trying to write a jQuery function to send a query string to a PHP script, but I can’t seem to get the text to get to the server in the correct format. I want to send this query string (with the appropriate URL encoding):
data={"name":"Chris"}
where ‘data’ is always a JSON string. Using jQuery’s .ajax function I tried setting my data variable to
data: { 'data': {"name":"chris"} },
but PHP ends up getting:
data[name]=chris
What’s the proper way to send the data back to the server so that the JSON string is properly reserved, without having to hand-craft the string?
First, you’ll need to use json2.js because jQuery does not include the capability to output JSON, only to parse it, and the method we will be using is not supported in IE 6/7. Convert your JavaScript object to JSON:
Then you need to include that JSON-formatted string as request data:
Edit: An older version of this post referred to the jquery-json plugin, but it’s obvious that that plug-in was written when jQuery 1.3.x was current.