Pass additional parameter to a JSONP callback

后端 未结 4 786
逝去的感伤
逝去的感伤 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:29

    There's a easier way. Append the parameter to your url after '?'. And access it in the callback function as follows.

    var url = "yourURL";
        url += "?"+"yourparameter";
        $.jsonp({
            url: url,
            cache: true,
            callbackParameter: "callback",
            callback: "cb",
            success: onreceive,
            error: function () {
                console.log("data error");
            }
        });
    

    And the call back function as follows

    function onreceive(response,temp,k){
      var data = k.url.split("?");
      alert(data[1]);   //gives out your parameter
     }
    

    Note: You can append the parameter in a better way in the URL if you already have other parameters in the URL. I have shown a quick dirty solution here.

提交回复
热议问题