2017-03-01 96 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