Post JSON data with AJAX object

Get help using Construct 2

Post » Fri Jun 07, 2013 4:12 am

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.
B
7
Posts: 6
Reputation: 762

Post » Fri Jun 07, 2013 6:16 am

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.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Fri Jun 07, 2013 1:02 pm

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.asheron2013-06-07 13:10:42
B
7
Posts: 6
Reputation: 762

Post » Fri Jun 07, 2013 1:25 pm

You should URLEncode any data you post, regardless of the format of the data. So you should still URLEncode your JSON.
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842

Post » Fri Jun 07, 2013 2:45 pm

Ah, thank you very much. This explains a lot.asheron2013-06-07 14:45:29
B
7
Posts: 6
Reputation: 762

Post » Sat Jun 08, 2013 5:04 pm

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?

B
7
Posts: 6
Reputation: 762

Post » Sun Jun 09, 2013 12:49 am

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).
B
7
Posts: 6
Reputation: 762

Post » Sun Jun 09, 2013 1:28 pm

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?
Scirra Founder
B
395
S
233
G
88
Posts: 24,376
Reputation: 193,842


Return to How do I....?

Who is online

Users browsing this forum: NN81 and 19 guests