我想要做的是在 HTTP 协议上有一个页面,将 AJAX 调用发送到同一个 Web 服务器但使用 HTTPS。请求页面和 AJAX 处理程序都在同一台服务器上,具有相同的域和端口。(即,唯一的区别是协议。)为了说明,
来自 http://www.example.com/index.php
触发对https://www.example.com/authenticate.php?user=123&password=456的 jQuery AJAX 调用
(我希望通过 HTTPS 传递密码以使其在 Internet 上加密。由于一些设计限制,我必须使用 AJAX 调用而不是重定向页面。)
我知道这里存在 CORS 问题,因此我进行了一些研究,发现我实际上可以利用 Access-Control-Allow-Origin 标头来提供帮助。然后我在我的 Apache 的配置文件中设置以下内容,
Header set Access-Control-Allow-Origin *
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "content-type, accept"
Header set Access-Control-Max-Age 1000
我可以看到,当浏览器向服务器请求资源时,可以看到标头。要求:
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Authorization Basic Y2FzZXRhZ3JhbWRldjpwYXNzd29yZGRldiE=
Cache-Control no-cache
Connection keep-alive
Cookie __utma=99230732.2019724749.1337107099.1337849971.1337856946.9; __utmz=99230732.1337107099.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=217650581.954519005.1337107174.1337772401.1337777327.5; __utmz=217650581.1337107174.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=99230732; PHPSESSID=m8lnqhqv2qa6f884a8um413n81
Host www.example.com
Pragma no-cache
Referer http://www.example.com/index.php
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
回应就像,
Accept-Ranges bytes
Access-Control-Allow-Head... content-type, accept
Access-Control-Allow-Meth... GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Orig... *
Access-Control-Max-Age 1000
Connection close
Content-Length 16599
Content-Type application/x-javascript
Date Thu, 24 May 2012 14:48:17 GMT
Etag "48157-40d7-4c0c938b220c0"
Last-Modified Thu, 24 May 2012 14:39:39 GMT
Server Apache/2.2.3 (CentOS)
所以看起来标题部分已经完成。(我对吗?)
然后,当我尝试在 JavaScript 中调用以下 jQuery AJAX 代码时,
$.ajax({
// Use HTTPS as there is password transferred
url : "https://www.example.com/authentication.php",
type : 'POST',
dataType : 'json',
async : false,
data : ajaxData,
beforeSend : function(xhr, opt) {},
error : function(error) {
console.log("Ajax error: unable to login user: ");
console.log(error);
},
success : function(status) {
if(status==USER_AUTH_AUTHENTICATE_USER_SUCCESS) {
console.log("User login succeeded!");
} else {
console.log("User login failed.");
}
}
});
浏览器(FireFox 12)只会返回一个对象,
readyState 0
status 0
statusText "[Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js Line: 8240"]"
有什么我错过的吗?
事实上,我已经尝试了很多来自论坛和博客的建议,试图完成这项工作,但我仍然没有成功。我也尝试使用 JSONP,它在 FireFox 上运行良好,但在 Chrome/Safari 上失败,没有可读的错误消息,但只是从 jQuery 代码“head.insertBefore(脚本,head.firstChild);”中抛出错误。
如果有人能给我一个关于我的代码/设置有什么问题的线索,我将不胜感激。
谢谢!
编辑于 2012-05-25 20:29 (UTC +08:00)
正如建议的那样,我阅读了这个参考案例(http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working),我发现它链接对于这种情况(http://stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery)也是相关的。我在那里尝试了示例 XHR 代码,
var request = new XMLHttpRequest();
var params = "action=something";
request.open('POST', 'https://www.example.com/controllers/Authentication.php', true);
request.onreadystatechange = function() {if (request.readyState==4) alert("It worked!");};
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.send(params);
该代码是从具有 HTTP 协议的页面调用的。执行代码后,立即抛出以下错误,
访问受限 URI 被拒绝...test_xhr.php 第 11 行
(如果我在 JavaScript HTTP 中更改 HTTPS,脚本会立即运行,因此应该不会有任何语法问题。)
页面本身的请求头和响应头如下。要求是,
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Authorization Basic Y2FzZXRhZ3JhbWRldjpwYXNzd29yZGRldiE=
Cache-Control no-cache
Connection keep-alive
Cookie __utma=99230732.2019724749.1337107099.1337856946.1337921578.10; __utmz=99230732.1337107099.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=217650581.954519005.1337107174.1337772401.1337777327.5; __utmz=217650581.1337107174.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=99230732; PHPSESSID=ktd6anojfi40ohemlujosdmhi4
Host www.example.com
Pragma no-cache
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0
回应是,
Access-Control-Allow-Head... content-type, accept
Access-Control-Allow-Meth... GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Orig... *
Access-Control-Max-Age 1000
Connection close
Content-Length 590
Content-Type text/html; charset=UTF-8
Date Fri, 25 May 2012 12:24:44 GMT
Server Apache/2.2.3 (CentOS)
X-Powered-By PHP/5.1.6
所以我只是在想我的设置有一些根本性的问题,但不是 jQuery,因为原生 XHR 不能正常工作。:(