Pass additional parameter to a JSONP callback

后端 未结 4 771
逝去的感伤
逝去的感伤 2020-12-17 17:47

For a project of mine I need to do multiple calls to a (remote) API using JSONP for processing the API response. All calls use the same callback function. All the calls are

4条回答
  •  醉梦人生
    2020-12-17 18:16

    This is a year old now, but I think jfriend00 was on the right track, although it's simpler than all that - use a closure still, just, when specifying the callback add the param:

    http://url.to.some.service?callback=myFunc('optA')

    http://url.to.some.service?callback=myFunc('optB')

    Then use a closure to pass it through:

    function myFunc (opt) {
        var myOpt = opt; // will be optA or optB
    
        return function (data) {
            if (opt == 'optA') {
                // do something with the data
            }
            else if (opt == 'optB') {
                // do something else with the data
            }
        }
    }
    

提交回复
热议问题