2011-12-19 46 views
0

我有一些从API请求一些JSON的代码。当数据返回时,根据文档,它发回一个回调函数,该函数将用于解析顶级数据。呼叫作出后,我有以下代码捕获数据,并对其进行处理:将Params传递给一个窗口[回调]函数

var callback = 'functionUsedInApiCall'; 
window[callback] = newCallBackFunction; 

我怎么会去通过自订参数给回调函数上面的数据被返回?

为了捕获数据,我必须写这样的回调函数:

function newCallBackFunction(root) { 
    //root is the data 
} 

任何帮助将不胜感激。

回答

2

你在说什么JSONP?如果是这样,根本不调用回调或传递参数,API返回的代码会。

例如,你的代码:

window.myCallback = newCallbackFunction; 

function newCallbackFunction(data) { 
    // use the data 
} 

(我假设这是不是在全球范围内,因此分配给window对象。)

...加上你的启动代码JSONP调用,它通常在页面中添加一个script元素,其中包含回调名称的URL(上面的“myCallback”)。

他们的反应会是这样的:

myCallback({ 
    // data here 
}); 

...,当它到达,将运行(因为它是一个script元素的内容),并调用你的函数。这是JSONP的工作原理。


如果要包括进一步参数的功能,你要做的就是有他们叫转身打电话给你的目标函数的回调,如:

window.myCallback = function(data) { 
    newCallbackFunction(data, "foo", "bar"); 
}; 

function newCallbackFunction(data) { 
    // use the data 
} 

现在,当他们的代码调用全球myCallback,它所做的全部事情都是转身,并用您指定的参数拨打电话newCallbackFunction

这些参数不必像上面那样是文字。下面是一个有点上下文的示例,使用closure

// Assume the url already contains the name of the callback "myCallback" 
function doJSONP(url, niftyInfo, moreNiftyInfo) { 
    var script; 

    // Set up the callback 
    window.myCallback = function(data) { 
     // Runs when the data arrives 
     newCallbackFunction(data, niftyInfo, moreNiftyInfo); 
    }; 

    // Trigger the request 
    script = document.createElement('script'); 
    script.src = url; 
    document.documentElement.appendChild(script); 
} 

理想的情况是,做JSONP当你每次自动生成回调的名称,以便它是具体的要求(如果你有在同一时间两个突出的请求):

// Assume the url ends with "callback=" and we append the name of the 
// callback function to it 
function doJSONP(url, niftyInfo, moreNiftyInfo) { 
    var cbname, script; 

    // Get a callback name 
    cbname = "callback_" + 
       new Date().getTime() + 
       "_" + 
       Math.floor(Math.random() * 10000); 

    // Set up the callback 
    window[cbname] = function(data) { 
     // Remove us from the window object 
     try { 
      delete window[cbname]; 
     } 
     catch (e) { // Handle IE bug (throws an error when you try to delete window properties) 
      window[cbname] = undefined; 
     } 

     // Runs the function 
     newCallbackFunction(data, niftyInfo, moreNiftyInfo); 
    }; 

    // Trigger the request 
    script = document.createElement('script'); 
    script.src = url + encodeURIComponent(cbname); 
    document.documentElement.appendChild(script); 
} 
+0

这是完美的!正是我在找什么!是的,我为每个特定的调用自动生成,只是不知道如何在触发时将其他数据传入回调。谢谢你的帮助! – spez86 2011-12-19 15:46:46

+0

@ spez86 :-)很高兴帮助! – 2011-12-19 15:49:23

0

参数在JavaScript作为数组传递,这样你就可以通过你需要将你的回调每箱功能添加的参数,甚至是功能齐全。

你可以做到以下几点:

function newCallBackFunction(root /*your data*/, paramsHash /*a hash array with optional parameters*/) 
{ 
    //if arg1 can be found in the hash 
    if(paramsHash['arg1'] ] 
    { 
     //do something that requires arg1 
    } 
    else if(paramsHash['arg2']) 
    { 
     //do something that requires arg2 
    } 


    return root; 

} 

而在你的主代码:

var hash = new Array(); 
hash['arg1'] = 'str1'; 
hash['arg2'] = 1; 
hash['arg3'] = new Car(); //or any other object you want 

它也可能只是声明一些参数,并将其提供给您的功能只在需要时:

function newCallBackFunction(root, param1, param2) 
{ 
    if(param1) { /* similar to first example */ } 
}  

或者最后只是传递你想要的任何参数并从参数中读取它们表

function newCallBackFunction(root) 
{ 
    for(int i = 1; i < arguments.length; i++) 
    //do something with the parameters you pass beside root 
}  

而且在主代码:

newCallBackFunction(root, param1, param2, param3); 

我希望我罩着你!

+0

*“JavaScript中的参数作为数组传递”*不,它们不是。传递参数的基本机制是依赖于实现的,但它们最终成为调用函数时生成的*变量环境*的*绑定对象的属性(参见[10.4.3](http:// es5.github.com/#x10.4.3)和规范10.5)。 'arguments'对象不是一个数组(虽然它有一些类似数组的行为),并且在大多数实现中,只有在函数中使用它(以运行时成本,在某些引擎上为重要的引擎)时才会创建它。 – 2011-12-19 16:00:29