日期:2014-05-16  浏览次数:20470 次

跨域请求之JSONP 四

此次主要是接口调整与代码重构。

?

接口如下

?

// 开启调试模式
Sjax.debug = true;

Sjax.get({
    url       // 请求url 
    param     // 请求参数 (键值对字符串或js对象)
    success   // 请求成功回调函数
    failure   // 请求失败回调函数
    scope     // 回调函数执行上下文
    timestamp // 是否加时间戳
});
?

?

此外增加了调试模式。后台仍然返回的是jsonp全局变量。示例如下

?

<!DOCTYPE HTML>
<html> 
<head> 
	<meta charset="utf-8"> 
	<title>sjax_0.4.js by snandy</title>
	<script src="sjax_0.4.js"></script>
</head> 
<body>
<input type="button" value="Get Name 1" onclick="clk1()"/>
<input type="button" value="Get Name 2" onclick="clk2()"/>

<script type="text/javascript">
Sjax.debug = true;
function clk1(){
	Sjax.get({
		url : 'http://localhost:9000/www/servlet/json',
		param : {name:'33',age:44},
		success : function(){console.log(jsonp.name)},
		failure : function(){console.log('error');}
	});
}
function clk2(){
	Sjax.get({
		url : 'jsonp66.js',
		success : function(){console.log(jsonp.name)},
		failure : function(){console.log('error');}
	});
}
</script>
</body>
</html>