T
T
TMLEX2017-06-04 13:33:37
JavaScript
TMLEX, 2017-06-04 13:33:37

Why does javascript ajax send an empty POST request?

Good afternoon everyone, I can’t understand why an empty request is sent if JSON data is sent, but if not JSON is sent, but, for example, in application/x-www-form-urlencoded encoding, then everything is fine. jQuery is not suggested.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if(xhr.readyState==4) {
      console.log(xhr.responseText);
    }
  }
xhr.open("POST", urli, false);
var json = JSON.stringify({ user_id: user, tag_id: tag }, null, '\t'); console.log(json); //Здесь JSON выводится в консоли нормально
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.send(json);
//Переменные user, tag, urli указаны, с этим проблем нет.

Here is the code for the file that ajax accesses:
<?php
if (isset($_POST))
var_dump($_POST);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2017-06-04
@devspec

Maybe send to another domain? Cross-domain requests are difficult to support.

T
TMLEX, 2017-06-04
@TMLEX

No, to that domain. The problem is with the JSON encoding. Change the format and accordingly the passed variable - everything works fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question