跨域JS与服务器端通信例子
A域(a.com)
存在采集接口 http://www.a.com/gather.aspx?采集参数1=采集参数值1&采集参数2=采集参数值2&......&采集参数n=采集参数值n。
操作:采集信息入库
在Page_Load事件中,例如:
string s="";
for(int i=0;i<this.Request.QueryString.Count;i++){
s+="\t"+this.Request.QueryString.AllKeys[i]+":"+this.Request.QueryString[i];
}
if(s!="")
{
s+="\t";
}
s+="IP:"+this.Request.UserHostAddress;
s+="\t时间:"+System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
ArrayList al = null;
if(Application["gatherlist"]==null)
{
al = new ArrayList();
Application["gatherlist"]=al;
}
al = (ArrayList)(Application["gatherlist"]);
if(al.Count>9) al.RemoveAt(0);
al.Add(s);
B域(b.com)
存在业务逻辑判断接口 http://www.b.com/operationlogic.aspx?业务参数1=业务参数值1&业务参数2=业务参数值2&......&业务参数n=业务参数值n。
操作:判断业务参数,返回业务逻辑判断结果和采集接口URL以及参数信息。
在Page_Load事件中,例如:
string s="http://a.com/gather.aspx";
if(this.Request.QueryString.Count>0){
s+="?";
}
for(int i=0;i<this.Request.QueryString.Count;i++)
{
if(i==0)
{
s+=this.Request.QueryString.AllKeys[i]+"="+this.Request.QueryString[i];
}
else{
s+="&"+this.Request.QueryString.AllKeys[i]+"="+this.Request.QueryString[i];
}
}
string s1="f_show('"+s+"');f_gather('"+s+"');";
this.Response.Write(s1);
f_show-回调C域客户端显示业务逻辑判断结果
f_gather-回调B域JS方法中的JS采集接口
存在用于C域IE客户端与B域服务器业务逻辑接口通信的JS文件。
操作:动态生成用于通信的JS脚本,支持业务逻辑判断接口回调的采集JS接口方法。
JS文件(test.js)内容例如:
function f_script(){
var js_obj = document.createElement( "script" );
js_obj.type = "text/javascript" ;
js_obj.setAttribute( "src" , "http://www.b.com/operationlogic.aspx?id=123&cid=234");
document.body.appendChild(js_obj);
}
function f_gather(s){
var js_obj = document.createElement("span");
var objname="span_gather"
js_obj.id = objname;
js_obj.innerHTML="<iframe src='"+s+"' width=0 height=0 frameborder=no></iframe>";
document.body.appendChild(js_obj);
}
f_script();
C域(c.com)
通过JS调用B域业务逻辑判断接口,传入业务参数,获取业务逻辑判断结果和采集接口信息,显示业务逻辑判断结果并调用采集接口信息。
IE客户端代码例如:
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language=javascript>
function f_show(s){
document.getElementById("span_show").innerHTML=s;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<span id="span_show"></span>
</form>
<script language=javascript src="
http://www.b.com/test.js"></script>
</body>
整个流程下来,可以实现JS调用与服务器端的通信
爱东东
我的各类资源收集收集网站 http://tag.iddgame.com
我的实验性技术网站 http://www.iddgame.com
我的魔兽世界网站 http://www.iddwow.com.cn