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!

0

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

1 Comment

@g9m29 You're welcome !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.