Ajax请求的奇怪行为 [英] Weird behaviour with Ajax request

查看:182
本文介绍了Ajax请求的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,使多个ajax获取。

  for @ data data 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查看有哪些索引
console.log dataType
indices = []
对于数据中的对象
indices = indices.concat .keys(object))
console.logindices
console.log indices
arr = @typeIndexMap [dataType]
如果arr
@typeIndexMap [dataType] = @typeIndexMap [dataType] .concat(indices)
else
@typeIndexMap [dataType] = indices
console.logtypeIndexMap
console.log @typeIndexMap

dataType中的console.log总是返回@dataTypes中的最后一个dataType,尽管事实上console.log dataType第一个函数显示两者,表明循环正在发生。



我打印出了url - 它们都不同,但我得到的响应是完全相同的,如果最后一个dataType附加到someUrl和多个是用那个网址。



为什么会发生这种情况?我认为这与回调的本质有关。

解决方案

您的问题是您的成功 callback:

  success:(data)=> @populateSearchIndices(data,dataType)

只是抓取 dataType 作为引用,直到回调被触发才会被求值。发生这种情况时, dataType 将是 @dataTypes 数组中的最后一个值,所有回调将使用



您需要强制 dataType 在循环体中求值,CoffeeScript具有 do


当使用JavaScript循环生成函数时,通常插入一个闭包,以确保循环变量被关闭,并且所有生成的函数不仅仅共享最终值。 CoffeeScript提供了 do 关键字,它立即调用传递的函数,转发任何参数。


所以你想要更像这样的东西:

  for dataType in @dataTypes 
do(dataType) - > ;
url = someUrl + dataType
#...如前面

看看相应的JavaScript,你会看到你的循环体被转换为一个函数,它以 dataType 作为参数调用,函数包装器和执行力<



您的 url(code> code> dataType c>

$

 $ 

 

url = someUrl + dataType

,然后在 $。ajax 调用而不是拖动引用。


I have a loop that is making multiple ajax gets.

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

The console.log in dataType always returns the last dataType in @dataTypes, despite the fact that the console.log dataType in the first function displays both, suggesting that a loop is occurring.

I printed out the url as well - they're both different, but the response I get is the exact same as if the last dataType were appended to someUrl and multiple gets were made with that url.

Why is this happening? I think this has something to do with the nature of callbacks.

解决方案

Your problem is that your success callback:

success : (data) => @populateSearchIndices(data, dataType)

is just grabbing dataType as a reference that won't be evaluated until the callback is triggered. By the time that happens, dataType will be the last value in the @dataTypes array and all of your callbacks will use that same value.

You need to force dataType to be evaluated in the loop body and CoffeeScript has do for that:

When using a JavaScript loop to generate functions, it's common to insert a closure wrapper in order to ensure that loop variables are closed over, and all the generated functions don't just share the final values. CoffeeScript provides the do keyword, which immediately invokes a passed function, forwarding any arguments.

So you want something more like this:

for dataType in @dataTypes
  do (dataType) ->
    url = someUrl + dataType
    #... as before

If you have a look at the corresponding JavaScript, you'll see that your loop body is converted to a function that is called with dataType as a parameter, the function wrapper and execution forces dataType to be evaluated (rather than just referenced) for each loop iteration.

Your url behaves as expected because you evaluated dataType when you build it:

url = someUrl + dataType

and then use it in the $.ajax call rather than dragging a reference around.

这篇关于Ajax请求的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆