function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { // 此时即支持CORS的情况 // 检查XMLHttpRequest对象是否有“withCredentials”属性 // “withCredentials”仅存在于XMLHTTPRequest2对象里 xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// 否则检查是否支持XDomainRequest,IE8和IE9支持 // XDomainRequest仅存在于IE中,是IE用于支持CORS请求的方式 xhr = new XDomainRequest(); xhr.open(method, url);
哪怕这些 ip 都被绑定在一个域名上
2,如果IE8以上,回调函数这样不可以啊。
this.getXmlHttpObject(url);
this.xmlHttp.open("POST", url, false);
this.xmlHttp.onload=function()
{
callBack(jma);
}
this.xmlHttp.send(data);
怎么才能做回调函数呢?谢谢
我就想知道回调函数,每次我方法执行完了,返回以后,回调函数才执行。
这是js代码
this.requestSoapData = function(url,data,callBack,jma) {
debugger;
this.getXmlHttpObject(url); //实例化XDomainRequest
this.xmlHttp.open("POST", url, false);
// this.xmlHttp.onreadystatechange=callBack;
this.xmlHttp.progres=function()
{
callBack(jma);
}
this.xmlHttp.send(data);
},
//下面是要请求的方法
this.queryMapBookerInput = function(){
debugger;
var sendData = this.envelopQueryMapBookerInput();
this.jma.requestSoapData(this.url,sendData,this.callBackQueryMapBookerInput,this);
return this.bookMarkerArr;
},
//这是回调函数
this.callBackQueryMapBookerInput = function(soapText){
var qmbo = "QueryMapBookerOutput";
var xmlDoc = soapText.jma.retXmlDoc(soapText.jma.xmlHttp.responseText);
soapText.bookMarkerArr = soapText.jma.getQueryMapBooker(xmlDoc,qmbo);
},
我都是调用queryMapBookerInput这个方法的
他类似于xmlHttp的onreadystatechange方法吗?
这是用ActiveXObject对象创建的。当他open的时候open("POST", url, false);
我用fidder拦截,它连请求都没法送过去。服务端已经设置header了,我这边服务都是好的。
应该是ActiveXObject对象本身对url进行了拦截。
我就想知道回调函数,每次我方法执行完了,返回以后,回调函数才执行。
这是js代码
this.requestSoapData = function(url,data,callBack,jma) {
debugger;
this.getXmlHttpObject(url); //实例化XDomainRequest
this.xmlHttp.open("POST", url, false);
// this.xmlHttp.onreadystatechange=callBack;
this.xmlHttp.progres=function()
{
callBack(jma);
}
this.xmlHttp.send(data);
},
//下面是要请求的方法
this.queryMapBookerInput = function(){
debugger;
var sendData = this.envelopQueryMapBookerInput();
this.jma.requestSoapData(this.url,sendData,this.callBackQueryMapBookerInput,this);
return this.bookMarkerArr;
},
//这是回调函数
this.callBackQueryMapBookerInput = function(soapText){
var qmbo = "QueryMapBookerOutput";
var xmlDoc = soapText.jma.retXmlDoc(soapText.jma.xmlHttp.responseText);
soapText.bookMarkerArr = soapText.jma.getQueryMapBooker(xmlDoc,qmbo);
},
我都是调用queryMapBookerInput这个方法的ws可以使用Response对象进行输出。注意使用Response.End结束,具体参考这个:jquery jsonp请求asp.net webservice
这是用ActiveXObject对象创建的。当他open的时候open("POST", url, false);
我用fidder拦截,它连请求都没法送过去。服务端已经设置header了,我这边服务都是好的。
应该是ActiveXObject对象本身对url进行了拦截。ie不能用acx,要用XDomainRequest,自己判断兼容性。。
这是用ActiveXObject对象创建的。当他open的时候open("POST", url, false);
我用fidder拦截,它连请求都没法送过去。服务端已经设置header了,我这边服务都是好的。
应该是ActiveXObject对象本身对url进行了拦截。ie不能用acx,要用XDomainRequest,自己判断兼容性。。对于IE我真心想骂了
1,麻烦您看一下这个帖子http://hi.baidu.com/qnaaceyaqlbklzq/item/fe82b7f0e83cfe683c1485f3
我跟他的问题是一样的,但是我不能写服务。。
2,我用XDomainRequest的话,原来用xmlhttp的都要重写。XDomainRequest调用回调函数返回数据,我还不清楚怎么实现。举个例子,我给提供接口A,A有一个回调函数B。 外面调用的时候,就是var a = new A();返回一个数据。但是我数据只能在B里处理。我现在调用A的时候,B根本没走,就返回回来了望解答。。
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// 此时即支持CORS的情况
// 检查XMLHttpRequest对象是否有“withCredentials”属性
// “withCredentials”仅存在于XMLHTTPRequest2对象里
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// 否则检查是否支持XDomainRequest,IE8和IE9支持
// XDomainRequest仅存在于IE中,是IE用于支持CORS请求的方式
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// 否则,浏览器不支持CORS
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}
http://blog.csdn.net/hfahe/article/details/7730944
那么不是服务器对服务器访问吗?
这与浏览器有什么关系?
解决浏览器跨域,都是要设置信任域的。而设置信任域就增加了风险你在浏览器上的应用通过自己的服务器中转就可以了
谢谢这个我早就弄好了。。我的疑问是,我给提供接口A,A有一个回调函数B。 外面调用的时候,就是var a = new A();返回一个数据。但是我数据只能在B里处理。我现在调用A的时候,B根本没走,就返回回来了
客户端为A,A访问B(b只有js和html访问C)。
客户端为A,A访问B(b只有js和html访问C)。你管不了c,就需要在b做代理了,b的html请求你的代理页面,而不是直接请求cXDomainRequest IE自己的东东,说是ie8支持,其他的版本不知道支持部支持。。下面这个在IE7,IE8下测试通过 var xdr = new XDomainRequest();
xdr.onload = function () { alert(xdr.responseText) }
xdr.open('post', 'http://www.coding123.net/json.asp');
xdr.send(null)
客户端为A,A访问B(b只有js和html访问C)。你管不了c,就需要在b做代理了,b的html请求你的代理页面,而不是直接请求cXDomainRequest IE自己的东东,说是ie8支持,其他的版本不知道支持部支持。。下面这个在IE7,IE8下测试通过 var xdr = new XDomainRequest();
xdr.onload = function () { alert(xdr.responseText) }
xdr.open('post', 'http://www.coding123.net/json.asp');
xdr.send(null)
//我想retValue,但是onload不知道什么时候调用
this.getConnection = function(user){
var retValue ="";
var sendData = this.envelopConnetion(user);
var xdr = new XDomainRequest();
xdr.onload = function () {
retValue = xdr.responseText;
}
xdr.open('post', this.url);
xdr.send(sendData);
},
var retValue = "";
var sendData = this.envelopConnetion(user);
var xdr = new XDomainRequest();
/* xdr.onload = function () {
retValue = xdr.responseText;
}*/
xdr.open('post', this.url, false); //改为同步
xdr.send(sendData);
return xdr.responseText;/////
}
谢谢这个我早就弄好了。。我的疑问是,我给提供接口A,A有一个回调函数B。 外面调用的时候,就是var a = new A();返回一个数据。但是我数据只能在B里处理。我现在调用A的时候,B根本没走,就返回回来了可能走的缓存,也可能出现js错误。