XHR Wrapper POST方法不起作用

时间:2017-09-29 04:22:19

标签: javascript ajax http post xmlhttprequest

我试图在javascript中执行Wrapper XMLHTTPRequest。我做了GET方法,它工作正常,但是当涉及到POST方法时,它给了我一个错误:

  

POST http://127.0.0.1:8080/text2.txt 405(方法不允许)

     

XHR加载失败:POST“http://127.0.0.1:8080/text2.txt”。

我正在运行一个带节点的本地http服务器

我有一个星期坚持这个问题,我真的想知道我怎么能让这个工作

这是我的js代码:

var wrapper = new XMLHttpRequest();
var params = 'Name=Cesar';

function getText2() {
    wrapper.open('POST', 'text2.txt');
    wrapper.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    wrapper.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        document.getElementById('Name').value = this.responseText;
        }
    };
    wrapper.send(params);
}

这是我的html文档:

<!DOCTYPE html>
<html>
    <head>
        <script src="XHR.js"> </script>
    </head>
    <body>

        <h2>WRAPPER</h2>

        Name: <input type="text" id="Name"/>

        <button type="button" onclick="getText2()">
        Post Test</button>

    </body>
</html>

0 个答案:

没有答案
相关问题