Post JSON data with AJAX object

From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.

    Hello,

    I'm attempting to use the AJAX object to post JSON data.

    The write-up for the AJAX object mentions that the URLEncode system expression should be used for urlencoded content, but is there a similar expression for JSON content?

    If not, any reason manually formatting the data wouldn't work? I understand the double quotes requirement. For example, in the data field:

    {"title":"Hello back at you"}

    would need to be:

    {""title"":""Hello back at you""}

    Thanks for any insights.

    Correct me if I am wrong, but the double quote requirement should be used if you enter manually a string (as the " is understanding as the beginning/end of a string), otherwise, there is no need to double quote, since the variable is already a full string

    I can be wrong.

    Right, this assumes I was entering the data in the data field itself (which expects a string). The first set of quotes is a requirement of the JSON data format.

    You should URLEncode any data you post, regardless of the format of the data. So you should still URLEncode your JSON.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads

    Ah, thank you very much. This explains a lot.

    Hmm, the target of my AJAX post is expecting application/json as its content-type.

    It looks like the AJAX post content type in C2 uses application/x-www-form-urlencoded.

    Is this correct? If so, anyway to change the content-type for Post action in the AJAX object?

    To answer my own question, yes.

    To change the content-type in the AJAX plugin, you merely need to edit the AJAX runtime.js file and replace application/x-www-form-urlencoded with the content you need (in my case application/json).

    You should never edit the source of the official plugins, you will cause really annoying compatibility problems for yourself. Isn't it possible to read the data on the server-side and convert it to JSON even with that MIME type?

    How do I change that code?

    This thread is 6 years old and is now out of date. Use the 'Set request header' action to change the Content-Type.

    You saved me, thank you so much

    Okey so, I made that, I'm assuming on the value field I have to write the data I want to post, but how I send this data to a URL? If I use the "post to URL" how I should use it to send the previous json?

    Please start a new thread, this thread is 6 years old and out of date.

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