如何在 Javascript 中请求受密码保护的页面
我正在开发一个非常简单的侧栏小工具来分析我的路由器的每月带宽使用情况并确定该月的领先或落后程度。数据位于路由器托管的受密码保护的网页中(如果重要的话,我正在使用 DD-WRT)。我想使用 Javascript 将页面请求以及所有身份验证信息传递到路由器,以便一次性检索页面,但我似乎找不到正确的语法。有什么想法吗?
到目前为止,这是我的代码:
var ua = navigator.userAgent.toLowerCase();
if (!window.ActiveXObject){
req = new XMLHttpRequest();
}else if (ua.indexOf('msie 5') == -1){
req = new ActiveXObject("Msxml2.XMLHTTP");
}else{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('GET', 'http://192.168.1.1/Status_Internet.asp', false, "username", "password");
req.send(null);
if(req.status == 200){
dump(req.responseText);
} else{
document.write("Error");
}
document.write("Second Error");
Firebug 表明它在 req.send(null)
上引发错误 - 具体来说, 访问受限 URI 被拒绝”代码:“1012
。
可能是因为同源策略,但是在这种情况下我可以使用什么来代替 xmlhttpRequest 呢?
I'm working on a very simple Sidebar Gadget to analyze my router's monthly bandwidth usage and determine how far ahead or behind I am for that month. The data is located in a router-hosted, password protected webpage (I'm using DD-WRT, if it matters). I'd like to pass a page request to the router with Javascript, along with all the authentication information, to retrieve the page all in one go, but I can't seem to find the proper syntax for that. Any ideas?
Here's my code so far:
var ua = navigator.userAgent.toLowerCase();
if (!window.ActiveXObject){
req = new XMLHttpRequest();
}else if (ua.indexOf('msie 5') == -1){
req = new ActiveXObject("Msxml2.XMLHTTP");
}else{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('GET', 'http://192.168.1.1/Status_Internet.asp', false, "username", "password");
req.send(null);
if(req.status == 200){
dump(req.responseText);
} else{
document.write("Error");
}
document.write("Second Error");
Firebug indicates that it throws an error on req.send(null)
- specifically,Access to restricted URI denied" code: "1012
.
It may be because of the same-origin policy, but in that case what can I use instead of an xmlhttpRequest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为同源策略,替代方案是 iframe,但这并不能真正满足您的需求。
如果是 http-auth,您过去可以使用 http://username:pass@site 请求页面但我必须承认我已经很长时间没有尝试使用它了,所以我不知道它是否仍然受支持。
编辑:
如果这不起作用,也许您可以使用基本的 http 身份验证,如下所述: http://en .wikipedia.org/wiki/Basic_access_authentication 但这需要您起诉服务器端代理,因为当 xhr 不是一个选项时您无法操作来自 javascript 的请求标头。
It is because of the same-origin policy, the alternative is an iframe but that will not really give you what you wish for.
If it is http-auth you used to be able to request the page with http://username:pass@site but i must admit i haven't tried to use that for a long time, so i don't know if it is still supported.
EDIT:
If this doesn't work, maybe you can use basic http auth as described here: http://en.wikipedia.org/wiki/Basic_access_authentication but this would require you to sue a serverside proxy, since you can't manipulate the request headers from javascript when xhr is not an option.
您需要向请求添加
Authorization
标头。我不是 AJAX 专家,所以我不确定是否可以修改 AJAX 请求中的标头字段。如果你做不到,那么你就注定失败。如果可以的话,那么这篇维基百科文章包含一个示例它必须是什么样子。
You need to add a
Authorization
header to the request. I'm not an AJAX expert, so I'm not sure if you can modify header fields in AJAX requests. If you can't, then you're doomed.If you can, then this Wikipedia article contains an example what it must look like.
这是一个安全功能:您看,有一些恶意脚本使用类似的技术来破解路由器(谁会更改路由器密码?显然不是 Joe X. Schmoe)。
根据同源策略,AJAX 请求仅限于其发起的域(和端口):因此,您无法从本地页面向 192.168.1.1:80 发出请求。
有一个可能的解决方法 - 服务器端代理(例如 PHP 脚本,用于获取您的网络内的路由器页面)。
That's a security feature: you see, there are malicious scripts that used a similar technique to hack the routers (who changes their router password anyway? Apparently not Joe X. Schmoe).
Under the Same Origin Policy, AJAX requests are limited to the domain (and port) whence they originated: therefore, from a local page, you can't make a request to 192.168.1.1:80 .
There is a possible workaround - a server-side proxy (e.g. a PHP script that fetches the router pages for you) inside your network.
如果同源策略不是问题,那么
在您的页面上加载 jQuery 。
并使用 jQuery 的 $.ajax 方法。
有关详细信息,请参阅 jQuery ajax API。
If the same-origin policy is not the issue, then
load jQuery on your page.
and use jQuery's $.ajax method.
See the jQuery ajax API for details.