如何使用javascript和html发送发帖请求字段?

时间:2018-11-01 17:11:19

标签: javascript html post request xmlhttprequest

我想问一下如何使用JavaScript和HTML通过发帖请求将这些字段发送到网站,以获取HTTP响应201。

> {"returnUrl":"x","cancelUrl":"x","offerPaypalCredit":false,"experienceProfile":
> {"brandName":"test.com","noShipping":"true","addressOverride":false},"testVersion":"test/web/3.31.0","_meta":{"merchantAppId":"www.test.com","platform":"web","sdkVersion":"3.31.0","source":"client","integration":"custom","integrationType":"custom","sessionId":"zzzzz-zzzz-zzzzz-zzzzz-Zzzzzzz"},"authorizationFingerprint":"zzzzzzzzzz|created_at=zzzzzzmerchant_id=zzzzzzzzz&public_key=zzzzzzzz"}

我编写的代码:-

    <html>
    <body>
    <center>
    <h2>Payhere</h2>

    <div id="demo">
    <button type="button" onclick="send()">Send</button>
    </div>

    <script>
    function send() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = alert(this.responseText);
    }
    };
    xhttp.open("GET", "https://test.com", true);
    xhttp.withCredentials = true;
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");   xhttp.send(JSON.stringify({"returnUrl":"x","cancelUrl":"x","offerPaypalCredit":false,"experienceProfile":{"brandName":"test.com","noShipping":"true","addressOverride":false},"testVersion":"test/web/3.31.0","_meta":{"merchantAppId":"www.test.com","platform":"web","sdkVersion":"3.31.0","source":"client","integration":"custom","integrationType":"custom","sessionId":"zzzzz-zzzz-zzzzz-zzzzz-Zzzzzzz"},"authorizationFingerprint":"zzzzzzzzzz|created_at=zzzzzzmerchant_id=zzzzzzzzz&public_key=zzzzzzzz"}));
    }
    </script>

    </body>
    </html>

此代码无法正常工作,并给我200,但我希望201可以回复。

0 个答案:

没有答案