(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,90); font-family:微软雅黑; line-height:30px"> url : “http://www.xxx.com/ajax.do”,90); font-family:微软雅黑; line-height:30px"> dataType : “jsonp”,90); font-family:微软雅黑; line-height:30px"> jsonp: “callbackparam”,//服务端用于接收callback调用的function名的参数
jsonpCallback:”success_jsonpCallback”,//callback的function名称
success : function(json){
alert(json);
alert(json[0].name);
},90); font-family:微软雅黑; line-height:30px"> 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\"}])”);
}
原文链接:https://www.f2er.com/ajax/165375.html