2017-03-01 125 views
0

我有一個網頁,我需要獲取指定pastebin文件的原始數據,我們只需說http://pastebin.com/qnNPx6G9,並將其作爲變量存儲。我已經嘗試了xml和ajax請求中的許多變體,但沒有任何效果。這是我嘗試過的。我究竟做錯了什麼?HTML/Javascript-從原始pastebin獲取數據

我試着使用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。

+0

AJAX不能越過domain.so你不能在不同的域名網站使用AJAX。 –

+0

與這個問題無關,但在調試JSON時務必檢查['console.log'](https://developer.mozilla.org/en/docs/Web/API/Console/log) - 您的瀏覽器devtools可能還會顯示一些更有用的信息:) – Scott

+1

[JQuery Ajax請求到http://pastebin.com/raw.php](http://stackoverflow.com/questions/16449492/jquery-ajax-request)的可能重複-to-HTTP-引擎收錄-COM-RAW-PHP) – nicholas79171

回答

0

您正在嘗試做一個CORS請求引擎收錄顯然不允許作爲控制檯顯示了這個錯誤:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

我認爲你唯一的選擇是使用服務器端編程語言,以遠程訪問pastebin,只有在遠程服務器授權的情況下才允許CORS請求,否則您無法繞過它。

瞭解更多關於CORS here