HTML / Javascript-从原始pastebin获取数据

问题描述 投票:0回答: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。

javascript jquery ajax pastebin
1个回答
0
投票

您正在尝试发出一个CORS请求,显然不允许pastebin显示此错误:

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

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

了解更多关于CORS here的信息

© www.soinside.com 2019 - 2024. All rights reserved.