简体   繁体   English

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

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

I have a webpage in which I need to just get the raw data of a specified pastebin file, let's just say http://pastebin.com/qnNPx6G9 , and store it as a variable. 我有一个网页,我只需要获取指定pastebin文件的原始数据,就可以说http://pastebin.com/qnNPx6G9 ,并将其存储为变量。 I've tried many, many, many variations on xml and ajax requests, but nothing works. 我已经尝试了许多关于xml和ajax请求的变体,但是没有任何效果。 Here's what I've tried. 这是我尝试过的。 What am I doing wrong? 我究竟做错了什么?

I've tried with 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.

And with regular XMLHttpRequest: 并使用常规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.

Please help! 请帮忙! I'm sorry if this is a stupid question or anything is unclear, I'm not that good at understanding APIs. 很抱歉,如果这是一个愚蠢的问题或不清楚的地方,我不太了解API。 Thanks! 谢谢!

And no, I can't use PHP. 不,我不能使用PHP。

You are trying to make a CORS request that pastebin obviously doesn't allow as console shows up this error: 您正在尝试发出一个浆糊显然不允许的CORS请求,因为控制台显示此错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。

I think your only option is to use a server-side programming language in order to access pastebin remotely, CORS requests are only allowed just in the case the where remote server authorised it, else you have no way to bypass it. 我认为您唯一的选择是使用服务器端编程语言以便远程访问pastebin,仅在远程服务器授权它的情况下才允许CORS请求,否则您将无法绕过它。

Read more about CORS here 在这里阅读更多有关CORS的信息

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

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