POST JSON DATA WITH AJAX OBJECT

1 favourites
  • 10 posts
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • How do I change the content-type of the post in ajax to:application/json, and how do I send this json to an URL later?

  • The AJAX object has "Set Request Header" which allows you to set the header for your next request

  • Okey, then I want to send some data like that: {

    “email”: “string”,

    “firstName”: “string”,

    “lastName”: “string”,

    “nickname”: “string”,

    “position”: “string”,

    “score”: 0

    }

    where all the string parameters were introduced by the user, and I don't know how to write this on the Data parameter of the Ajax function "POST", I'm doing this right know, but i think it isn't correct

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You would still POST the data as Content-Type isn't the same as POST/GET.

    On the assumption you're using PHP, you're back-end code would look something like:

    $data = json_decode(file_get_contents('php://input'), true); print_r($data);

    Ps. Sorry it's not on new lines, not sure how to do it in the forum :-)

  • So the Ajax POST element doesn't work for me?

  • Could you upload your project file if you don't mind?

    I'd be surprised if there's a problem with the AJAX plugin, more likely you've got a minor error somewhere causing it.

  • Your screenshot shows an invalid expression, because the double quotes are wrong. In general you can't paste JSON unmodified in to an expression, because " starts and ends string expressions, and the " around JSON properties will cause syntax errors. You have a few options to work around that:

    - use "" inside a string to produce a single character without ending the string

    - paste it in to a Text object or some other plaintext object and use that object's text as an expression

    - paste it in to a project file and load it with AJAX

  • Okey so, first, thank you all for your help. It all seems to be working well, but I have an issue, when I change the header to "application/json", it looks like it is not changing the header, but adding it to it, so when I look in my server, the Content type i'm sending is: application/x-www-form-urlencoded, application/json, and it ruins all, is any way to remove this application/x-www-form-urlencoded?

  • I may be wrong but I believe you need to set it to "Content-Type: application/json"

  • Yes, I've done that but it has the two of them now, not just de json. Anyways, I changed my server to get the url type. Thank you for your help

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