方式一:远程加载js文件获取数据
(1) script 标签
<script src=’test.js’></script>
或
<script src=” id=”s1″></script>
<script language=”javascript”>
s1.src=”test.js”
</script>
或
<script>
var oHead = document.getElementsByTagName(‘HEAD’).item(0);
var oScript= document.createElement(“script”);
oScript.type = “text/javascript”;
oScript.src=”test.js”;
oHead.appendChild( oScript);
</script>
(2) 动态加载
JSLoader
JSLoader.loadJavaScript(“/ExtTestPro/WebContent/resources/libs/ext/adapter/ext/ext-base.js”);
或
jQuery.getScript(“http://app.cntvs.com/test/js.txt”, function(){…});
方式二:使用jsonp获取数据
客户端:
$.ajax({
type : “get”,
async:false,
url : “http://www.xxx.com/ajax.do”,
dataType : “jsonp”,
jsonp: “callbackparam”,//服务端用于接收callback调用的function名的参数
jsonpCallback:”success_jsonpCallback”,//callback的function名称
success : function(json){
alert(json);
alert(json[0].name);
},
error:function(){
alert(‘fail’);
}
});
服务端返回数据的示例代码:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = “text/plain”;
String callbackFunName = context.Request["callbackparam"];
context.Response.Write(callbackFunName + “([ { name:\"John\"}])”);
}