gpt4 book ai didi

ajax - 为什么我的 Jquery ajax 成功处理程序被数组(而不是响应对象)调用

转载 作者:行者123 更新时间:2023-12-01 03:40:58 25 4
gpt4 key购买 nike

我有一个函数可以对谷歌电子表格API进行两次休息调用。我使用 $.when 来确保在处理第二次调用的数据之前处理第一次调用的数据。

问题是第一个ajax处理程序(getRealNames)接收一个javascript对象作为其参数,但第二个处理程序(displayTeams)接收一个数组,第0个元素是我期望获取的对象。

为什么一个得到一个对象,另一个得到一个数组?他们正在调用相同的rest api。直到我重构代码以使用延迟而不是回调嵌套时,该数组才出现。所以我认为这是一个jquery问题而不是spreadsheetAPI问题。

(参见下面的屏幕截图,我已对两个处理程序收到的参数进行了 console.log

//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);

}

enter image description here

最佳答案

最终,更仔细地阅读 API 文档 ( http://api.jquery.com/jQuery.when/ ),我们在代码示例中发现了以下注释;

// 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 ]

我已阅读第一条评论,并假设参数只是返回数据。第二条评论揭示了我的问题的根源。

关于ajax - 为什么我的 Jquery ajax 成功处理程序被数组(而不是响应对象)调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20567528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com