2

通过 发布数据的问题XMLHttpRequest

对于GitHub Gist,这可以成功:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    document.getElementById("demo").innerHTML +=
        this.readyState + " " + this.status + "<br>";
};

var data = {
  "public": true,
  "files": {
    "file_name.txt": {
      "content": "Hello, world!"
    }
  }
}

xhr.open("POST", "https://api.github.com/gists", true);
xhr.send(JSON.stringify(data));

输出:

1 0
2 201
3 201
4 201

其中 201 - 成功代码。

但是对于Pastebin ,此代码显示出意外的行为:在状态 1 (OPENED) 跳转到状态 4 (DONE) 之后。

try {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        document.getElementById("demo").innerHTML +=
            this.readyState + " " + this.status + "<br>";
    };

    xhr.open("POST", "http://pastebin.com/api/api_post.php", true);
    xhr.send();

} catch (err) {
    window.alert(err.message);
}

输出:

1 0
4 0

我已阅读有关同源政策的信息,并认为这可能是我的问题的原因。是真的?是否可以修复与 pastebin.com 的交互?谢谢!

4

0 回答 0