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
  • 3,931
  • 7
  • 49
  • 99

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
  • 30,526
  • 12
  • 76
  • 90