1

I want to send a JSON object to PHP server but I get an empty array there. Why is this happening?

var obj = {
    username: username,
    password: password
};

var ajax = new XMLHttpRequest();
ajax.open('post', 'ajax/login_register.php');
ajax.setRequestHeader("Content-type", "application/json;charset=UTF-8");
ajax.send(JSON.stringify(obj));
M1X
  • 4,971
  • 10
  • 61
  • 123

1 Answers1

3

You need to give it a name that you can reference on the server side.

ajax.send('user=' + encodeURIComponent(JSON.stringify(obj)));

$_POST['user'] // <-- your JSON string
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91