为什么我的Jquery ajax成功处理程序是用数组调用的(而不是响应对象)
我有一个功能,可以对谷歌电子表格API进行两次休息调用。我使用$ .when来确保第一次调用的数据在处理第二次调用的数据之前处理。
I have a function which makes two rest calls to the google spreadsheetAPI. I use a $.when to make sure that the data fron thefirst call is process before dealing with the data from the second call.
问题是第一个ajax处理程序(getRealNames),接收一个javascript对象作为其参数,但第二个处理程序(displayTeams)接收一个数组,第0个元素是我期望得到的对象。
The problem is that the first ajax handler(getRealNames), receives a javascript object as its argument, but the second handler(displayTeams), receives an array, the 0th element is the object I was expecting to get.
为什么一个人获得一个对象而另一个获得一个数组?他们正在呼唤同样的休息api。
直到我重构代码以使用延迟而不是回调嵌套,数组才出现。所以我认为这是一个jquery问题,而不是一个spreadsheetAPI问题。
Why does one get an object and the other get an array? They are calling the same rest api. The array did not appear until I refactored the code to use deferreds instead of callback nesting. So I think this is a jquery question rather than a spreadsheetAPI question.
(参见下面的屏幕截图,我已经控制了两个处理程序收到的参数。 / p>
(see screen shot below, I've console.log'ed the arguments received by both handlers
//this is the function generating the REST requests, I just put it in for completeness
function getWorkSheet(doc_key,sheet){
return $.get('https://spreadsheets.google.com/feeds/list/'+
doc_key+'/'+sheet+
'/private/full?alt=json&access_token='
+ googleAPItoken)
.fail(function(){
alert("failed to get google doc:"+doc_key+" ,sheet: "+sheet);
});
}
function getRWMTeams() {
var nameQuery=getWorkSheet(doc_key,1);
nameQuery.done(getRealNames);
var repoQuery=getWorkSheet(doc_key,2);
//the deferred:'namesProcessed' is resolved in getRealNames
$.when(repoQuery,namesProcessed)
.done(displayTeams);
}
最后,仔细阅读api doc( http://api.jquery.com/jQuery.when/ )在代码示例中显示以下注释;
Eventually, more careful reading of the api doc(http://api.jquery.com/jQuery.when/) revealed the following comment in a code sample;
// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
// Each argument is an array with the following structure: [ data, statusText, jqXHR ]
I had read the first comment, and assumed that the arguments were simply the return data. The second comment reveals the source of my problem.