HTML/Javascript-从原始pastebin获取数据
我有一个网页,我只需要获取一个指定的pastebin文件的原始数据,比方说,并将其存储为一个变量。我尝试了很多很多关于xml和ajax请求的变体,但都没有效果。这是我试过的。我做错了什么 我尝试过Ajax:HTML/Javascript-从原始pastebin获取数据,javascript,jquery,ajax,pastebin,Javascript,Jquery,Ajax,Pastebin,我有一个网页,我只需要获取一个指定的pastebin文件的原始数据,比方说,并将其存储为一个变量。我尝试了很多很多关于xml和ajax请求的变体,但都没有效果。这是我试过的。我做错了什么 我尝试过Ajax: $.ajax({ url: "http://pastebin.com/api/api_post.php", type: "GET", dataType: "x-www-form-urlencoded", data: { "api_dev_key": "mydevkey", "
$.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。您试图发出一个pastebin显然不允许的CORS请求,因为控制台显示此错误: 请求的服务器上不存在“Access Control Allow Origin”标头 资源 我认为您唯一的选择是使用服务器端编程语言来远程访问pastebin,只有在远程服务器授权的情况下才允许CORS请求,否则您无法绕过它
阅读有关CORS的更多信息ajax不能跨域。因此,您不能在不同的域站点中使用ajax。这与问题无关,但您应该在调试时检查,而不是提醒JSON-您的浏览器开发工具可能还会显示一些更有用的信息:)可能重复