跨域请求之JSONP 二


续上篇,加两个实用功能

1,增加data属性,请求参数
2,增加scope属性,可以让回调函数在指定的上下文中执行

 

接口如下

1
2
3
4
5
6
Sjax.load(url, {
    data        // 请求参数 (键值对字符串或js对象)
    success     // 成功后回调函数
    scope       // 回调函数执行上下文
    timestamp   // 是否加时间戳
});

 

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>sjax_0.2.js by snandy</title>
    <script src="http://files.cnblogs.com/snandy/sjax_0.2.js"></script>
</head>
<body>
<input type="button" value="Get Name" onclick="clk()"/>
<script type="text/javascript">
    function clk(){
        Sjax.load('http://files.cnblogs.com/snandy/jsonp.js', {
            fn : function(){alert(jsonp.name)},
            data : {a:'1',b:'2',c:[11,22]},
            timestamp : true
        });
    }
</script>
</body>
</html>


https://github.com/snandy/io

原文地址:https://www.cnblogs.com/dullbaby/p/5341142.html