簡體   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