我被困在同步跨域调用。
在我们吃域呼叫我的应用比较早,所以没有问题
为使呼叫我先前的JavaScript代码是如下:
function EKXMLProvider(oDropdown, sDefault, sXML, sFilterUrl, fireRequestOnce)
{
var oXMLHTTP, i, length, oData, sValue, sDisplay, sName, sMatch, oRegExp;
if (!oDropdown)
return;
// XMLHTTP Object to retrieve the xml document
oXMLHTTP = this.createXMLHttpRequest();
this.FilterUrl = sFilterUrl;
if (sFilterUrl != previousFilterUrl){
oXMLHTTP.open("GET", sFilterUrl, false);
oXMLHTTP.send(null);
sFilterData = oXMLHTTP.responseText
previousFilterUrl = sFilterUrl;
}
if(!fireRequestOnce ||(fireRequestOnce && retrievedData == null))
{
this.documentUrl = sXML;
oXMLHTTP.open("GET", this.documentUrl, false);
oXMLHTTP.send(null);
oData = oXMLHTTP.responseXML.documentElement.childNodes;
if(fireRequestOnce)
retrievedData = oData;
}
else if(retrievedData != null)
{
oData = retrievedData;
}
this.suggestData = new Array();
// Filter out all 2 and 3 letter codes (airport, city, country)
oRegExp = new RegExp("\\s+\\(\\w{2,3}\\)", "gi");
var iCount = 0
for (i = 0, length = oData.length; i < length; i++)
{
sValue = oData[i].attributes.getNamedItem("v").value;
sDisplay = oData[i].attributes.getNamedItem("d").value;
sName = oData[i].attributes.getNamedItem("n").value;
//sMatch = oData[i].attributes.getNamedItem("m").value;
sMatch = oData[i].attributes.getNamedItem("e").value;
if (sFilterData.search(sValue) != -1){
this.suggestData[iCount] = new EKSuggestData(sName + " (" + sValue + ")", sDisplay, sValue, sMatch, sMatch.replace(oRegExp, ""));
iCount++;
}
}
// Call the inherited class
EKSuggestProvider.call(this, oDropdown, sDefault);
}
现在,当我们搬到了我们对不同的域的呼叫,我们需要做跨域调用,我改变了上面如下跨域代码:
function EKXMLProvider(oDropdown, sDefault, sXML, sFilterUrl, fireRequestOnce)
{
var oXMLHTTP, i, length, oData, sValue, sDisplay, sName, sMatch, oRegExp;
var qr = "&jsonpcall=true";
if (!oDropdown)
return;
// XMLHTTP Object to retrieve the xml document
oXMLHTTP = this.createXMLHttpRequest();
this.FilterUrl = sFilterUrl;
if (sFilterUrl != previousFilterUrl){
//alert(sFilterUrl);
//oXMLHTTP.open("GET", sFilterUrl, false);
//oXMLHTTP.send(null);
//sFilterData = oXMLHTTP.responseText
// queue up an ajax request
$.ajax({
url: sFilterUrl + qr,
type: "GET",
cache: true,
async:false,
contentType: "application/javascript; charset=utf-8",
dataType: "jsonp",
jsonpCallback: "airport",
success: function(data, textStatus, jqXHR)
{
if (data.airport[0] != '')
{
sFilterData = data.airport[0];
}
}
});
previousFilterUrl = sFilterUrl;
}
if(!fireRequestOnce ||(fireRequestOnce && retrievedData == null))
{
//alert(sXML);
this.documentUrl = sXML;
//oXMLHTTP.open("GET", this.documentUrl, false);
//oXMLHTTP.send(null);
// queue up an ajax request
$.ajax({
url: sXML + qr,
type: "GET",
async:false,
cache: true,
contentType: "application/javascript; charset=utf-8",
dataType: "jsonp",
jsonpCallback: "airportxml",
success: function(data, textStatus, jqXHR)
{
var xmlDoc = $.parseXML(data.myresult);
oData = xmlDoc.documentElement.childNodes;
alert(oData);
}
});
//oData = oXMLHTTP.responseXML.documentElement.childNodes;
if(fireRequestOnce)
retrievedData = oData;
}
else if(retrievedData != null)
{
oData = retrievedData;
}
this.suggestData = new Array();
// Filter out all 2 and 3 letter codes (airport, city, country)
oRegExp = new RegExp("\\s+\\(\\w{2,3}\\)", "gi");
var iCount = 0
for (i = 0, length = oData.length; i < length; i++)
{
sValue = oData[i].attributes.getNamedItem("v").value;
sDisplay = oData[i].attributes.getNamedItem("d").value;
sName = oData[i].attributes.getNamedItem("n").value;
//sMatch = oData[i].attributes.getNamedItem("m").value;
sMatch = oData[i].attributes.getNamedItem("e").value;
if (sFilterData.search(sValue) != -1){
this.suggestData[iCount] = new EKSuggestData(sName + " (" + sValue + ")", sDisplay, sValue, sMatch, sMatch.replace(oRegExp, ""));
iCount++;
}
}
// Call the inherited class
EKSuggestProvider.call(this, oDropdown, sDefault);
}
上述jQuery的变化是工作正常,当我把“异步:假,”在我的电话,但是按我的知识,我们不能有同步调用的跨域,如果我更改为“异步:真”的号召,开始给错误就行了( for (i = 0, length = oData.length; i < length; i++))
作为ODATA需要装上第二个“airportxml”,它似乎两个呼叫都依赖于彼此,所以每当第一呼叫发送同时也适用于下一个电话也。
我用ajaxQueue这个还可以,但没有运气。
请建议我需要做什么样的变化。