访问被拒绝。请求安全页面时发生 JavaScript 错误
在页面 SomePage.aspx
上,通过 JavaScript 代码 (XMLHttpRequest),我调用 SecuredPage.aspx
使用下一个代码:
var httpRequest = GetXmlHttp();
var url = "https://myhost.com/SecuredPage.aspx";
var params = "param1=" + document.getElementById('param1').value +
"¶m2=" + document.getElementById('param2').value;
httpRequest.open("POST", url, true);
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpRequest.onreadystatechange = function() {
//Call a function when the state changes.
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
alert(httpRequest.responseText);
}
}
httpRequest.send(params); // HERE ACCESS IS DENIED.
//---------------------------------------------
function GetXmlHttp() {
var xmlhttp = false;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
// Code for Internet Explorer.
{
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
它抛出一个 访问被拒绝 错误。如果发送到http(http://myhost.com/SecuredPage.aspx),它工作正常。
如何才能解决这个问题呢?
On page SomePage.aspx
, by JavaScript code (XMLHttpRequest) I call SecuredPage.aspx
used next code:
var httpRequest = GetXmlHttp();
var url = "https://myhost.com/SecuredPage.aspx";
var params = "param1=" + document.getElementById('param1').value +
"¶m2=" + document.getElementById('param2').value;
httpRequest.open("POST", url, true);
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpRequest.onreadystatechange = function() {
//Call a function when the state changes.
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
alert(httpRequest.responseText);
}
}
httpRequest.send(params); // HERE ACCESS IS DENIED.
//---------------------------------------------
function GetXmlHttp() {
var xmlhttp = false;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
// Code for Internet Explorer.
{
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
It throws an Access is denied error. If send to http (http://myhost.com/SecuredPage.aspx), it works fine.
How is it possible to resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望通过 Ajax 获取 HTTPS 页面,您需要从同一个域,没有别的办法,只要用Ajax就可以了。这是因为同源政策。
也就是说,有很多方法可以不使用 Ajax 来做到这一点,例如,您可以使用框架。
另一种方法是使用 JSONP,但这要求您获取 JSON
:)第三种方法,对于生产网站来说往往不是很有用,但修补起来仍然很有趣,那就是使用 YQL 作为代理。
最后,您始终可以设置自己的服务器端代理,以便调用一个 HTTP 地址来获取 HTTPS 页面并将其发送出去,但如果可以避免的话,这很少是一个好的解决方案。
If you wish to fetch an HTTPS page via Ajax you need to do it from an HTTPS page on the same domain, there is no other way, as long as you use Ajax. This is because of the same origin policy.
That said, there are plenty of ways to do this not using Ajax, for instance you can use frames.
Another way is to use JSONP, but this requires that you are fetching, well, JSON :)
A third way, that tends not to be very useful for production websites, but still can be fun to tinker around with, is to use YQL as a proxy.
Lastly you can always set up a serverside proxy of your own, so that you call an HTTP address that fetches the HTTPS page and sends it on, but this is rarely a good solution if it can be avoided.
这是因为浏览器将
http
和https
视为 2 个不同的站点/域,因此您必须遵守同源策略。解决这个问题的一种方法是使用 jsonp。
This is because the browser considers
http
andhttps
as 2 different sites/domains, and therefore you have to adhere to the same origin policy.One way to solve it is using jsonp.
正如所说,您的问题是您的浏览器将此视为跨域请求。另一种适应这种情况的方法是设置一个 crossdomain.xml 文件,如下所示:
我不是此方法的专家,但我已经成功地使用了它。可以通过添加更多
allow-access-from
标签来添加其他域。你可能需要做一些摆弄。 YMMV。As it's been said, your problem is that your browser sees this as a cross domain request. Another way to accommodate this is to set up a crossdomain.xml file like this:
I'm not an expert on this method, but I have used it successfully. Other domains can be added by adding more
allow-access-from
tags. You may need to do some fiddling. YMMV.