1

I am trying to make some ajax post raw js script and I am using one of the questions asked here in stackoverflow but I encountered a problem.

var r = new XMLHttpRequest(); 
r.open("POST", "###URL###", true);
r.onreadystatechange = function () {
    if (r.readyState != 4 || r.status != 200) return; 
        console.log(r.responseText);
    };
console.log(price);
r.send("prize=" + prize);
return false;

When I check in chrome network the payload is sent correctly prize=632 but the result of the php script with $_POST['prize'] is empty. Where can the problem be?

Best regards!

g9m29
  • 373
  • 3
  • 16

1 Answers1

1

You are missing the headers:

var data = "prize=" + prize;
var r = new XMLHttpRequest(); 
r.open("POST", "###URL###", true);

// Send the expected headers information along with the request
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.setRequestHeader("Content-length", data.length);
r.setRequestHeader("Connection", "close");

r.onreadystatechange = function () {
    if (r.readyState != 4 || r.status != 200) return; 
        console.log(r.responseText);
    };
console.log(price);
r.send(data);
return false;
g9m29
  • 373
  • 3
  • 16
Stephan
  • 41,764
  • 65
  • 238
  • 329