我有一个循环,正在制作多个ajax获取。
for dataType in @dataTypes url = someUrl + dataType console.log(dataType) console.log url $.ajax( url : url type : 'GET' success : (data) => @populateSearchIndices(data,dataType) ) populateSearchIndices:(data,dataType)-> console.log "looking at what indexes are there" console.log dataType indices = [] for object in data indices = indices.concat(Object.keys(object)) console.log "indices" console.log indices arr = @typeIndexMap[dataType] if arr @typeIndexMap[dataType] = @typeIndexMap[dataType].concat(indices) else @typeIndexMap[dataType] = indices console.log "typeIndexMap" console.log @typeIndexMap
dataType中的console.log始终返回@dataTypes中的最后一个dataType,尽管第一个函数中的console.log dataType同时显示两者,表明正在发生循环。
我也打印出了url – 它们都不同,但我得到的响应与上一个dataType附加到someUrl完全相同,并且使用该url创建了多个gets。
为什么会这样? 我认为这与回调的本质有关。
你的问题是你的success
回调:
success : (data) => @populateSearchIndices(data, dataType)
只是抓取dataType
作为参考,在触发回调之前不会对其进行评估。 到发生这种情况时, dataType
将是@dataTypes
数组中的最后一个值,并且所有回调都将使用相同的值。
您需要强制在循环体中评估dataType
,而CoffeeScript已经为此do
以下事情:
当使用JavaScript循环生成函数时,通常会插入一个闭包装,以确保循环变量被关闭,并且所有生成的函数不仅仅共享最终值。 CoffeeScript提供了
do
关键字,它立即调用传递的函数,转发任何参数。
所以你想要更像这样的东西:
for dataType in @dataTypes do (dataType) -> url = someUrl + dataType #... as before
如果您查看相应的JavaScript,您将看到您的循环体转换为使用dataType
作为参数调用的函数,函数包装器和执行强制dataType
被评估(而不是仅仅被引用)循环迭代。
您的url
行为符合预期,因为您在构建它时评估了dataType
:
url = someUrl + dataType
然后在$.ajax
调用中使用它而不是拖动引用。
以上就是jQuery教程分享使用Ajax请求的奇怪行为相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/547402.html