1

This is not a duplicate of Send POST data using XMLHttpRequest.


I would like to post a JavaScript object in JS. I already read the following questions :

None answer to my question since I would like each item of my JS object becomes a $_POST row. Here is an example:

myObj = { key1:'value1', 'key2':'value2' }

r = new XMLHttpRequest();
r.onreadystatechange = callBack;
r.open('POST', url, 'async');
r.send(myObj);

Server side code:

var_dump($_POST);

On server side, I would like to get from PHP:

  • $_POST['key1'] set to 'value1'
  • $_POST['key2'] set to 'value2'

Instead, $_POST is empty.

Note that the previous code works when sending string as:

r.send("key=value");

What's strange is that the link above says it should work but it does not however although I'm using a modern browser.

Bot élecul
  • 379
  • 3
  • 13
Fifi
  • 3,360
  • 2
  • 27
  • 53
  • tag your question better - this is mostly php related – Adelin Jan 23 '20 at 08:55
  • 2
    Please don't SHOUT, it's considered rude. Also "does not work" is not a very good problem description, please take some time to refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please don't forget how to create a [mcve] to show us. – Some programmer dude Jan 23 '20 at 08:55
  • Yes, it works with r.send("key=label"); – Fifi Jan 23 '20 at 08:56
  • This is mostly php related => Seriously ??? – Fifi Jan 23 '20 at 08:56
  • how you send it vs how you get it, yes – Adelin Jan 23 '20 at 08:56
  • 1
    Could you provide server-side code, please? – Evgeny Skarlat Jan 23 '20 at 08:57
  • I edit the question. – Fifi Jan 23 '20 at 08:59
  • You aren't treating the post response in your ajax call, how do you know this didn't work ? – Cid Jan 23 '20 at 08:59
  • @Cid, the callBack function displays .responseText. – Fifi Jan 23 '20 at 09:01
  • 2
    If you think your previous question was closed incorrectly, you should edit it to clarify and then ask for it to be reopened. Don't post an identical question. – Barmar Jan 23 '20 at 09:10
  • @Temmu I alread read all the answers. None exactly answer to my question. I want to send a object and receive the object in $_POST. I don't want to send JSON or other stuff, I know how to do that. This is not my question. – Fifi Jan 23 '20 at 09:12
  • I'm pretty sure the answer you linked to is wrong, it shouldn't have been accepted. – Barmar Jan 23 '20 at 09:13
  • @Barmar, this is also what I'm thinking. StackOverflow refuses my great questions, and accepts wrong answers :-) Nice revange. – Fifi Jan 23 '20 at 09:15
  • There are 18 answers ... Anyway, you can't pass a live JS object, http can transfer text only. A simple way is to use ex. FormData object. – Teemu Jan 23 '20 at 09:17
  • 1
    @miken, yes and I clearly do not understand why this question has been closed. "This question is of topic", Are you kidding me ? Could you explain why the question is off topic ? – Fifi Jan 23 '20 at 19:33
  • @miken By the way, the link you mentioned as duplicate is absolutely not duplicated, this is not the same question. I read carefully both of them. Did you read the questions/answer before posting your comment or event marking this question as "off topic" ? I absolutely don't understand you guys. – Fifi Jan 23 '20 at 19:43
  • 1
    Why the question is closed? Everything is clear, I would like to post an alternative answer with FormData. – Bot élecul Jan 24 '20 at 07:52

1 Answers1

2

The answer you linked to that says you can send the object in r.send() is wrong. The documentation says that the argument to r.send() must be:

  • A Document, in which case it is serialized before being sent.
  • A BodyInit, which as per the Fetch spec can be a Blob, BufferSource, FormData, URLSearchParams, ReadableStream, or USVString object.

A plain object is not any of these.

You should convert the object to one of those types, and URL-encoded strings are generally used for ordinary data (FormData is generally only used when you need to upload files).

var params = Object.entries(myObj).map((key, val) => encodeURIComponent(key) + '=' + encodeURIComponent(val))
    .join('&');
r.send(params);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Why not using a [`FormData`](https://developer.mozilla.org/fr/docs/Web/API/FormData) ? (from your answer, "you *should* ..." could be "you *can* ...") – Cid Jan 23 '20 at 09:24
  • 1
    I've clarified that this is the usual type for ordinary strings. – Barmar Jan 23 '20 at 09:25