繁体   English   中英

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

[英]HTML/Javascript- get data from raw pastebin

我有一个网页,我只需要获取指定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。

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

所请求的资源上没有“ Access-Control-Allow-Origin”标头。

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

在这里阅读更多有关CORS的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM