无法获得HTTP 201响应

时间:2018-11-04 15:08:14

标签: javascript html http

我正在尝试从此代码获取HTTP 201响应,但无法获取。请帮助我解决与此代码相关的问题。

    <html>
    <body>
    <center>
    <h2>PAY</h2>

    <div id="demo">
    <button type="button" onclick="lol()">payhere</button>
    </div>

    <script>
   function lol() {
var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() {
if (this.readyState == 4 | this.status == 201) {
document.getElementById("demo").innerHTML = alert(this.responseText);
    }
    };
    xhttp.open("GET", "https://api.test.com/cors", true);
    xhttp.withCredentials = true;
    xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhttp.send(JSON.stringify('{"returnUrl":"x","cancelUrl":"x","offerCredit":false,"experienceProfile":{"brandName":"test.com","noShipping":"true","addressOverride":false},"testLibraryVersion":"test/web/3.31.0","_meta":{}'));
    }
    </script>

    </body>
    </html>

谢谢

1 个答案:

答案 0 :(得分:0)

2xx形式的Http状态实际上不是客户端希望的。它是服务器根据其编码方式发送的响应代码。 201主要表示已创建,但我可以返回200而不是说“确定”,这意味着应该执行任何代码。当缺少标题或您发送的内容不正确时,通常会发生错误。请在您的代码中添加accept标头,如下所示:

xhttp.setRequestHeader("Accept", "application/json");