0

I've done a lot of times before, so I'm honestly confused why this is failing to pass anything. I've tried printing results (script gets a response and prints the file).

function submit_form_inpage(path, data, watchForChange){
    alert(data);
    watchForChange = watchForChange || false;
    var request = new XMLHttpRequest();
    request.open('POST', path, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    if (watchForChange == true) {
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                document.write(request);
                if (request.status==200 && request.status < 400){
                    var xmldata=request.responseText //retrieve result as an XML object
                    alert("XML:" + xmldata);
                }
                else{
                    alert("An error has occured making the request:" + request.status );
                }
            }
        }
    }
    var temp_string = array_to_string_for_post(data);
    var temp = JSON.stringify(data);
    alert(temp);
    request.send(temp);
}

My php is

print_r($_POST);

and my result is

XML: Array ()

Despite the fact that data passed in (which is double-checked right before being sent by my alert) is

{"reason":"get_stuff","build_name":"test"}
lilHar
  • 1,735
  • 3
  • 21
  • 35

1 Answers1

3

You said you were sending form encoded data.

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

Then you sent:

temp = JSON.stringify(data);

JSON is application/json not application/x-www-form-urlencoded (and isn't natively supported by PHP anyway).

Either encode your data as application/x-www-form-urlencoded or correct your content-type and parse it manually in PHP.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks, I completely forgot I had left that in there when I started modifiying some of my other code. That's what I get for coding with a 101 degree fever. :P – lilHar Feb 04 '16 at 23:00
  • Sadly, that didn't fix it, although it was a needed change. – lilHar Feb 04 '16 at 23:11
  • @liljoshu — What, exactly, did you change and to what? – Quentin Feb 04 '16 at 23:16
  • I changed `application/x-www-form-urlencoded` to `application/json`. I feel like the problem is obvious and I'm overlooking it, but I'm just way to slow today. :/ – lilHar Feb 04 '16 at 23:22
  • 1
    See the last sentence of the answer. You got to the "and" and then stopped. – Quentin Feb 04 '16 at 23:24
  • That did it, adding the `$json = file_get_contents('php://input');` got me the variables, thanks! – lilHar Feb 04 '16 at 23:27