代码之家  ›  专栏  ›  技术社区  ›  chris_techno25

XMLHttpRequest从PHP编码的服务器返回406错误

  •  -1
  • chris_techno25  · 技术社区  · 5 年前

    所以我制作了这个应用程序,并在PHP 5.6xampp服务器上完美运行。很久以前,我把所有的东西都转移到了一个主机Gator网站上,我不记得它在哪个PHP版本上运行,但它还是按预期运行。但是,最近发生了错误,我已经跟踪了它,罪魁祸首是XMLHttpRequest response 406 error。所以我可以更容易地跟踪错误,我已经删除了额外的代码,只留下了可以重现错误的行。主机Gator目前只在5.4版本的PHP上运行。

    <?php
    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();
    }
    ?>
    

    现在这是我的客户端代码,它接受来自服务器的响应。。。

      var ajax = new XMLHttpRequest();
      ajax = new XMLHttpRequest();
      ajax.open("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);
        }else{
          alertify.notify(response.message, 'error', 3);
          closeEvent.cancel = true;
        }
      }
    

    它在我的服务器上运行良好,但在HostGator的服务器上抛出错误。有谁能帮我纠正这个错误吗?HostGator作为服务器在我的XAMPP上运行时,如何不接受响应?非常感谢。

    1 回复  |  直到 5 年前
        1
  •  0
  •   Quentin    5 年前

    一个 406 error 指:

    服务器无法生成与请求的主动内容协商头中定义的可接受值列表匹配的响应

    你说过 ajax.setRequestHeader("Accept", "application/json");

    您提供的代码中没有任何内容可以测试 Accept 头和输出406如果没有匹配。

    支票的来源是个谜,需要更多的信息。检查主机的文档和日志文件可能是一个好的开始。

    如果我要推测的话,我建议检查一下PHP脚本的输出。因为你没说 header("Content-Type: application/json"); text/html .

    HTML不是JSON,因此很明显,客户端不能接受输出。