2015-12-22 21 views
0

我试图从网站获取输出的文本。如果网站显示true具有某个组合标识的用户,它将显示用户标识。如果网站显示false,它将跳过该用户并转到下一个。控制台错误:请求的资源上没有“Access-Control-Allow-Origin”标题

我用下面的代码来处理上面列出的指示:

var countedOwners = 0, nameOfOwner; 
document.writeln("<h1>Found owners</h1>"); 

var setUp = function() { var assetID = prompt("Enter asset ID in the input box which you would like to search for","1285307"); 

if (assetID != null && assetID.length > 5 && assetID.length < 10 && !isNaN(assetID)) { 

var speed = prompt("How fast would you like to scan in milliseconds? - Do not type under 200 milliseconds for overload issues."); 

if (speed != null) { 

if (speed > 200) { 
var userID = prompt("What ID would you like to start from(will increase by 1 every time)) Default: 1", "1"); 
if (userID != null && !isNaN(userID)) { 
itemScanner(assetID, speed, userID); 
} 

} else { 

itemScanner(); 

} 
} 
}}; 


function createLink(userID, speed) { 
var a = document.createElement('a'); 
var linkText = document.createTextNode("http://www.roblox.com/User.aspx?ID=" + (userID - 1)); 
a.appendChild(linkText); 
a.title = "my title text"; 
a.href = "http://www.roblox.com/User.aspx?ID=" + (userID - 1); 
document.body.appendChild(a); 
//creating speed increaser... 
var btn = document.createElement("BUTTON"); 
var t = document.createTextNode("Increase speed?"); 
btn.appendChild(t); 
document.body.appendChild(btn); 
btn.onclick = function(){ var newSpeed = prompt("Your current speed is " + speed + " milliseconds. \n Input what speed you would like it to run at below.","300") 
speed = newSpeed; 
}; 
} 

var itemScanner = function(assetID, speed, userID) { 
setInterval(function() { 
$.ajax({ 
url: "https://api.roblox.com/ownership/hasasset?userId=" + userID + "&assetId=" + assetID, 
type: 'GET', 
}).success(function(Data) { 
var pageInfo = Data; 
if (pageInfo === true) { 
$.get("http://api.roblox.com/Users/" + userID, function(json) { 
nameOfOwner = json.Username; 
countedOwners = countedOwners + 1; 
console.info("Discovered owner of the item, the user's ID is " + userID + "." + "The name is " + nameOfOwner); 
console.info("Concurrent discovered owners - " + countedOwners + "."); 
document.write("<h4>Found an Owner ("+ "User ID = " + (userID - 1) + ")" + " Owner Name: " + nameOfOwner + "." + "</h4>"); 
createLink(userID, speed); 
}); 
} 
userID++; 
}); 
}, speed); 
} 


setUp(); 

然而,当我运行它通过在Roblox.com谷歌浏览器的控制台,它显示错误:

XMLHttpRequest cannot load https://api.roblox.com/ownership/hasasset?userId=undefined&assetId=undefined . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://www.roblox.com ' is therefore not allowed access. The response had HTTP status code 503.

我该如何解决这个问题?

+0

发生这种情况是因为你有一个外部地址,你想加载水平 - 在你的情况下,我认为它是www.roblox.com和api.roblox.com之间的问题。也许你需要在这里的.htaccess文件 – messerbill

+0

我不认为这会是一个问题,因为api.roblox.com是roblox.com的子域.. –

+0

什么是主机?它是roblox.com吗? – messerbill

回答

1

您请求的资源阻止了来自外部域的请求,它通过设置HTTP标头来完成此操作,此标头为Access-Control-Allow-Origin,通过查看它出现的错误消息头部设置,这意味着它没有Access-Control-Allow-Origin:*如果你从不同的域调用你将需要。

这里有更多的关于CORS a link

我假设你在你试图访问的可能是错误的页面的控制不是,另一种解决方案可能是添加接入控制 - 允许 - 起源:http://www.roblox.com到这个网页。

+0

正如我在messerbill的回复中所提到的,api.roblox.com是roblox的一个子域名。而不是一个不同的域名,因此我认为它可以免除这个问题。 –

+0

就像我说的 - 我敢打赌,因为http/https跨域 - 看看这里:http://stackoverflow.com/questions/8414786/javascript-cross-domain-call-call-from-http- to-https – messerbill

+0

我不相信我需要https,如果我将它们全部更改为以http开头,它会解决任何问题吗? –

相关问题