1

我有一个网页,我需要在其中获取指定 pastebin 文件的原始数据,比如说http://pastebin.com/qnNPx6G9,并将其存储为变量。我已经尝试了很多很多关于 xml 和 ajax 请求的变体,但没有任何效果。这是我尝试过的。我究竟做错了什么?

我尝试过使用 Ajax:

$.ajax({
url: "http://pastebin.com/api/api_post.php",
type: "GET",
dataType: "x-www-form-urlencoded",
data: {
    "api_dev_key": "mydevkey",
    "api_option": "paste",
    "api_paste_code": "blah blah"
},
success: function(res) {
    alert(JSON.stringify(res));
},
error: function(res) {
    alert(JSON.stringify(res));
}
});
//this is in the form of create paste, because I was seeing if it would work where get did not- it didn't.

并使用常规 XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('POST'/*also tried GET*/, 'http://pastebin.com/raw/qnNPx6G9', true); //I've also tried /raw.php?i=qnNPx6G9
xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        alert(this.responseText);
      }
};
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send("api_option=trends&api_dev_key=DEVKEY");
//I tried trends because creating a paste and getting a paste didn't work.

请帮忙!如果这是一个愚蠢的问题或不清楚的地方,我很抱歉,我不太擅长理解 API。谢谢!

不,我不能使用 PHP。

4

1 回答 1

1

您正在尝试发出 pastebin 显然不允许的 CORS 请求,因为控制台显示此错误:

请求的资源上不存在“Access-Control-Allow-Origin”标头。

我认为您唯一的选择是使用服务器端编程语言来远程访问 pastebin,仅在远程服务器授权的情况下才允许 CORS 请求,否则您无法绕过它。

在此处阅读有关 CORS的更多信息

于 2017-12-20T05:55:44.880 回答