XMLHttpRequest Returns 406 Error From PHP Coded Server

So I have this application made and perfectly running on my PHP 5.6 XAMPP server. A long time ago I transferred everything to a Host Gator website and I don't remember which PHP version it was running on but it ran as expected. However, recently errors occurred and I have traced it and the culprit is XMLHttpRequest response 406 error. So I can trace the error easier, I have removed the extra codes and just left the lines that would reproduce the error. Host Gator currently only runs on 5.4 version PHP.

This is my PHP code...This basically returns a JSON response.

require 'sql.php';
require 'securitycheck.php';
$response = array();

try {

    //temporarily removed the codes that get the data sent by the client... Even with these removed, shouldn't cause errors...
    $response["code"] = 1;
    $response["message"] = "File Accepted";

    echo json_encode($response);

} catch (OAuthException $e) {
    echo $e->getMessage();

Now this is my client side code which accepts the response from the server...

  var ajax = new XMLHttpRequest();
  ajax = new XMLHttpRequest();"POST",'ajax/savefile.php', false);
  ajax.setRequestHeader("Accept", "application/json");
  ajax.setRequestHeader('Content-Type', 'application/upload');
  ajax.setRequestHeader("Http-X-Requested-With", "XMLHttpRequest");
  ajax.setRequestHeader("Security-Key", securitykey);
  ajax.send("filename=" + savedname + "&filedata=" + JSON.stringify(canvas.toJSON()) + "&width=" + canvas.getWidth() + "&height=" + canvas.getHeight() + "&command=update");  

  if (ajax.status == 200){
    response = JSON.parse(ajax.responseText);
    if (response.code == 1){
      alertify.notify(response.message, 'success', 3);
      alertify.notify(response.message, 'error', 3);
      closeEvent.cancel = true;

It runs well on my server but throws an error on HostGator's server. Can anybody help me fix this error? How can the response not be accepted with HostGator as the server when it runs on my XAMPP? Thank you so much.



A 406 error means:

the server cannot produce a response matching the list of acceptable values defined in the request's proactive content negotiation headers

You said ajax.setRequestHeader("Accept", "application/json"); so you only accept JSON responses.

There's nothing in the code you provided which would test for the Accept header and output a 406 if nothing matched.

Where the check is coming from is something of a mystery that requires more information. Checking your host's documentation and log files would probably be a good start.

If I were to speculate then I'd suggest that something was checking the output of your PHP script. Since you haven't said header("Content-Type: application/json"); PHP will default to claiming it is outputting text/html.

HTML isn't JSON, so clearly the output isn't acceptable to the client.