我正在尝试使用Ajax进行跨域请求,并且服务器支持它,我添加了以下响应头:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
但是,XMLHttpRequest的状态代码仍为0,表示跨域请求失败。我正在使用Firefox 15.0.1,这是最新版本,所以它应该支持它。 谁能告诉我我在这里缺少什么?
这是我的代码:
function getAjaxObject(crossRequest)
{
if ((crossRequest)&&(window.XDomainRequest))
{
var domainRequestObject=new window.XDomainRequest();
domainRequestObject.onload=function() { };
domainRequestObject.onprogress=function() { };
domainRequestObject.ontimeout=function() { };
domainRequestObject.onerror=function() { };
return domainRequestObject;
}
else if (window.XMLHttpRequest!=null) return new XMLHttpRequest();
else return new ActiveXObject("Microsoft.XMLHTTP");
}
function sendOperationRequest(requestPath,requestXML,readyFunction,loadFunction)
{
ajaxObject=getAjaxObject(true);
if (window.XDomainRequest) ajaxObject.onload=loadFunction;
else ajaxObject.onreadystatechange=readyFunction;
//console.log(ajaxObject.onload);
ajaxObject.open("POST",requestAddress + requestPath,true);
if (!window.XDomainRequest)
{
ajaxObject.setRequestHeader("Content-Type","application/xml");
ajaxObject.setRequestHeader("Content-Length",requestXML.length);
}
else ajaxObject.contentType="application/xml";
ajaxObject.send(requestXML);
}
function login()
{
var username=document.getElementById("usernameInput").value;
var password=document.getElementById("passwordInput").value;
if (username=="") alert("Please enter the user name.");
else if (password=="") alert("Please enter the password.");
else sendOperationRequest("BackOfficeLoginServlet?UTF-8=\"yes\"",
"<backoffice-login-request user-name=\"" + username + "\" user-password=\"" +
password + "\" business-entity-id=\"" + businessEntity + "\"/>",
loginReadyStateChanged,handleLoginResponse);
}
function loginReadyStateChanged()
{
if (ajaxObject.readyState==4)
{
console.log(ajaxObject.status);
if (ajaxObject.status==200) handleLoginResponse();
else if (ajaxObject.status!=0)
{
alert("There is a connection problem. Make sure you " +
"are online and try again.");
}
}
}
这是一个指向日志文件的链接: https://skydrive.live.com/redir?resid=D3F36D33272D62D0!107