Why does jQuery.post get into PHP $ _POST and my mailbox is not working?

I use vanilla JavaScript to send an AJAX request with JSON data:

xhr.open(method, url,true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));

The headers look good, but in PHP it's $_POSTempty. There are several related questions about SO about this, like this one , but they all suggest using:

json_decode(file_get_contents("php://input"))

However, if I use jQuery.post, my variables fall into $_POST, so this should be possible. My question is: how? What can i do wrong? Or what can I change?

+4
source share
1 answer

, jQuery , , , application/x-www-form-urlencoded, , PHP supgllobal $_POST.

XMLHttpRequest JSON application/json, PHP $_POST.

formData , ajax PHP

+9

Source: https://habr.com/ru/post/1569740/


All Articles