解决方法 :
1. 在目标站 document.domain = 'somedomain.com';
并建立一个ajax.html,引用ajax方法 (大家都叫它服务中介)
然后创建一个ajax对象 var webreq = new Ajax();
2. 在本地站 document.domain = 'somedomain.com';
用iframe引用目标站的ajax.html.
详细方法:
1. 本地站的一个页面(Test.html)
html:
<html>
<head>
<script type='text/javascript'>document.domain='somedomain.com';</script>
<script type='text/javascript'>
function getAjax()
{
var spn = document.getElementByIdx('spninfo');
var bbsWin = document.getElementByIdx('ifrWindow').contentWindow;
var Ajax = bbsWin.webreq;
Ajax.Config.Result = 'TestAjaxCross';
Ajax.Config.returnType = 'Content';
Ajax.ActionPost('http://bbs.somedomain.com/doAjax.aspx',spn,window);
}
</script>
</head>
<body>
<span id='spninfo' />
<input type='button' id='ajaxBtn' οnclick='getAjax' value='Get' />
</body>
<iframe id='ifrWindow' src='http://bbs.somedomain.com/ajax.html' style='display:none;'></iframe>
</html>
/
2. 目标站的ajax.html
Html:
<html>
<head>
<script type='text/javascript'>document.domain='somedomain.com';</script>
<script type='text/javascript' scr='http://bbs.somedomain.com/ajaxMethod.js'></script>
<script type='text/javascript'>
var webreq = new Ajax();
function ExecResult(ty,W)
{
if (ty == 'TestAjaxCross')
{
W.document.getElementByIdx('loginName').value = '';
W.document.getElementByIdx('loginPass').value = '';
W.location.href = 'http://www.somedomain.com/urlyouwantgo';
}
}
</script>
</head>
<body>
</body>
</html>
----------------------------------------------------------------
目标站: doAjax.aspx
html为空
doAjax.aspx.cs
代码(Page_Load)
Response.Write("<zwd><content>看到效果了么?</content></zwd>");
return;
=====================================================
AjaxMethod.js 代码
var Try = {
these: function() {
var returnValue;
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) {}
}
//alert(123);
return returnValue;
}
}
function grr(rp) {
if(RegExp.$1)/(.*)/.exec("");
var re=new RegExp("<result>(.*)<//result>");
re.exec(rp);
if(RegExp.$1) return RegExp.$1;
return "";
}
function crr(rp) {
if(RegExp.$1)/(.*)/.exec("");
var re=new RegExp("<content>(.*)<//content>");
re.exec(rp);
if(RegExp.$1) return RegExp.$1;
return "";
}
var Ajax = function() {}
//var xhr ;这样定义不行
Ajax.prototype.Init = function(){
return Try.these(
function() {return new ActiveXObject("Msxml2.XMLHTTP")},
function() {return new ActiveXObject("Microsoft.XMLHTTP")},
function() {return new XMLHttpRequest()}
) || false;
}
Ajax.prototype.Config = {
Result:"",
SucInfo:"",
FaildInfo:"",
Url:"",
returnType:"Compare", //输入Compare是比较返回的字符是否一致,要指定Result值,//其他返回内容
ExecFunc:function(ty,W){
if (typeof ExecResult == 'function')
ExecResult(ty,W);
},
sendData:""
}
var aj = new Ajax();
Ajax.prototype.Action = function(url) { //同步
url = url + '&e='+Math.random();
var xhr = aj.Init();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
aj.FuncResult(aj.Config.Spn,xhr);
}
}
}
xhr.open("POST",url,false);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(null);
}
Ajax.prototype.Actionfor = function(url,spn) { //异步
url = url + '&e='+Math.random();
var xhr = aj.Init(); //这样写是为了多异步执行
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
aj.FuncResult(spn,xhr);
}
}
}
xhr.open("GET",url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(null);
}
Ajax.prototype.ActionAlert = function(url) //执行alert提示框的同步
{
url = url + '&e='+Math.random();
var xhr = aj.Init(); //这样写是为了多异步执行
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
aj.AlertResult(xhr);
}
}
}
xhr.open("GET",url,false);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(null);
}
Ajax.prototype.ActionPost = function(url,spn,W) //同步 send
{
url = url + '?e='+Math.random();
var xhr = aj.Init(); //这样写是为了多异步执行
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
aj.postResult(spn,xhr,W);
}
}
}
try {
if (netscape.security.PrivilegeManager.enablePrivilege)
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
}catch(e) {};
xhr.open("POST",url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//xhr.setRequestHeader("Content-Length",pars.length);
xhr.setRequestHeader("Connection", "open");
xhr.send(Webreq.Config.sendData);
}
Ajax.prototype.GetJSONData = function(url,spn)
{
url = url + '?e='+Math.random();
var xhr = aj.Init(); //这样写是为了多异步执行
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
aj.jsonResult(spn,xhr);
}
}
}
xhr.open("POST",url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//xhr.setRequestHeader("Content-Length",pars.length);
xhr.setRequestHeader("Connection", "open");
xhr.send(null);
}
Ajax.prototype.onResult = function(v) {
return v==aj.Config.Result;
}
Ajax.prototype.FuncResult = function(spn,xhr)
{ //alert(spn.id);
if (aj.Config.returnType == 'Compare')
{
if (aj.onResult(grr(xhr.responseText)))
{
spn.innerHTML = aj.Config.SucInfo;
aj.Config.ExecFunc(aj.Config.Result);
if (aj.Config.Url!='')
{
window.location.href = aj.Config.Url;
}
}
else
{
spn.innerHTML = aj.Config.FaildInfo;
}
}
else
{
spn.innerHTML = crr(xhr.responseText);
aj.Config.ExecFunc(aj.Config.Result);
}
}
Ajax.prototype.AlertResult = function(xhr)
{
if (aj.Config.returnType=='Compare')
{
if (aj.onResult(grr(xhr.responseText)))
{
alert(aj.Config.SucInfo);
aj.Config.ExecFunc(aj.Config.Result);
}
else
{
alert(aj.Config.FaildInfo);
}
}
else
{
alert(crr(xhr.responseText));
aj.Config.ExecFunc(aj.Config.Result);
}
}
Ajax.prototype.postResult = function(spn,xhr,W)
{
if (aj.Config.returnType == 'Compare')
{
if (aj.onResult(grr(xhr.responseText)))
{
spn.innerHTML = aj.Config.SucInfo;
aj.Config.ExecFunc(aj.Config.Result,W);
}
else
{
spn.innerHTML = aj.Config.FaildInfo;
}
}
else
{
spn.innerHTML = crr(xhr.responseText);
aj.Config.ExecFunc(aj.Config.Result,W);
}
}
Ajax.prototype.jsonResult = function(spn,xhr)
{
var jsonstr = xhr.responseText;
var json = eval_r("return " + jsonstr);
//get data json.something json:{username:"123",content:""};
aj.Config.ExecFunc(aj.Config.Result);
}
///
var WebServices = function() {}
WebServices.Config = {
}