How do I use Ajax to post json formatted string.

Get help using Construct 2

Post » Wed Feb 25, 2015 1:09 am

I can get "ajax post to url" to send data in the query string format, eg "foo=1&bar=2", but simple tests using a json string as data {"test":123} fail, is it possible to use json data as a ajax post payload, since have a more complex string in json format I want to send (example below):

{"Room":"Lobby","Data":[{"t":7.485703,"p":"A","e":"chat","a":"Joined the chat room as host"},{"t":11.571944,"p":"B","e":"chat","a":"joined"},{"t":18.800892,"p":"A","e":"chat","a":"hello"},{"t":25.592219,"p":"B","e":"chat","a":"peer hello"},{"t":30.018804,"p":"A","e":"chat","a":"chat text"},{"t":30.018804,"p":"A","e":"request-page","a":"page"},{"t":30.018804,"p":"A","e":"team-success","a":"team"},{"t":30.018804,"p":"A","e":"review","a":"review"},{"t":30.018804,"p":"A","e":"pass-resource","a":"param A","b":"param B"},{"t":30.018804,"p":"A","e":"select-choice","a":"param A","b":"param B"},{"t":30.018804,"p":"A","e":"move-resource","a":"param A","b":"param B","c":"param C"},{"t":30.018804,"p":"A","e":"enter-value","a":"param A","b":"param B","c":"param C"},{"t":30.018804,"p":"A","e":"solution-found","a":"param A","b":"param B","c":"param C"},{"t":30.018804,"p":"A","e":"rule-confirm","a":"param A","b":"param B","c":"param C"},{"t":30.018804,"p":"A","e":"output-change","a":"param A","b":"param B","c":"param C"},{"t":30.018804,"p":"A","e":"retry"},{"t":30.018804,"p":"A","e":"finish"},{"t":30.018804,"p":"both","e":"start"},{"t":30.018804,"p":"k2","e":"lost"}]}
B
7
Posts: 33
Reputation: 351

Post » Wed Feb 25, 2015 3:32 am

one solution, pass the whole json as a query pair, eg:

"json="&jsonDataString

then decode on in the PHP at the other end with:

$jsonIn=(isset($_POST['json'])) ? $_POST['json'] : "";
$jsonArray=json_decode($jsonIn,true);

and access using array notation.
B
7
Posts: 33
Reputation: 351


Return to How do I....?

Who is online

Users browsing this forum: Anna Nehves Br, divsyntax, volumetv and 17 guests