首页 新闻 会员 周边

如何传参给jsonp的回调函数

0
悬赏园豆:50 [已解决问题] 解决于 2016-08-26 17:28

示例代码如下:

function getAdT1(data1) {
    $.ajax({
        url: '...',
        type: 'get',
        dataType: 'jsonp',
        jsonpCallback: 'jsonpCallbackT1'
    });
}

function jsonpCallbackT1(json, data2) {    
}

请问如何将getAdT1的data1参数值传递给jsonpCallbackT1的data2参数?

js
dudu的主页 dudu | 高人七级 | 园豆:31007
提问于:2016-08-26 16:17
< >
分享
最佳答案
1

不是应该在success中写处理方法么

function getAdT1(data1) {
    $.ajax({
        url: '...',
        type: 'get',
        dataType: 'jsonp',
        jsonpCallback: 'jsonpCallbackT1',
        success: function(json) {
            jsonpCallbackT2(json, data1)
        }
    }

    function jsonpCallbackT2(json, data2) {}

 

收获园豆:30
刘宏玺 | 专家六级 |园豆:14020 | 2016-08-26 16:38

实际应用场景需要指定jsonpCallback

dudu | 园豆:31007 (高人七级) | 2016-08-26 16:52

@dudu: 

jsonpCallback
Type: String or Function()
Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.
 
如果是String类型的话应该没啥作用,可以随意指定,也就是说不删除那段代码也可以
刘宏玺 | 园豆:14020 (专家六级) | 2016-08-26 16:58

@刘宏玺: 赞!不去掉jsonpCallback: 'jsonpCallbackT1'就行。

dudu | 园豆:31007 (高人七级) | 2016-08-26 17:27
其他回答(2)
0

dudu问的,支持一下,混豆。。。

收获园豆:5
顾晓北 | 园豆:10844 (专家六级) | 2016-08-26 16:30
0

可以设置一个全局变量xxx,getAdT1里面把data1给xxx,jsonpCallbackT2直接取不就可以了啊

收获园豆:15
balahoho | 园豆:2050 (老鸟四级) | 2016-08-26 16:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册