How do I use Ajax to post json formatted string.

  • 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"}]}

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)