如何用jsp写一个ajax跨域代理
如何用jsp写一个ajax跨域代理
------解决方案--------------------转载自http://www.xyhhxx.com/news/net/20061013121041.htm
<html>
<head>
<script language= "javascript " type= "text/javascript ">
function loadContent()
{
var s=document.createElement( 'SCRIPT ');
s.src= 'http://www.anotherdomain.com/TestCrossJS.aspx?f=setDivContent ';
document.body.appendChild(s);
}
function setDivContent(v)
{
var dv = document.getElementById( "dv ");
dv.innerHTML = v;
}
</script>
</head>
<body>
<div id= "dv "> </div>
<input type= "button " value= "Click Me " onclick= "loadContent() ">
</body>
</html>
其中的www.anotherdomain.com/TestCrossJS.aspx是这样的,
<script language= "C# " runat= "server ">
void Page_Load(object sender, EventArgs e)
{
string f = Request.QueryString[ "f "];
Response.Clear();
Response.ContentType = "application/x-javascript ";
Response.Write(String.Format(@ "
{0}( '{1} '); ",
f,
DateTime.Now));
Response.End();
}
</script>
点击“Click Me”按钮,生成一个新的script tag,下载对应的 Javascript 脚本,结束时回调其中的setDivContent(),从而更新网页上一个div的内容。