简体   繁体   English

jQuery:如何将值传递给Ajax调用?

[英]jQuery: How do I pass a value into an Ajax call?

I am updating some div as follows: 我正在更新一些div如下:

for(var i = 0; i < data.length; i++) {
    var query = base_url + data[i];
    $.ajax({
        url: query,
        type: 'GET',
        dataType: 'jsonp',
        timeout: 2000,
        error: function() { self.html("Network Error"); },
        success: function(json) {
            $("#li" + i).html("<img src='" + json.result.list[0].url + "' />")
        }
    });
}

The value of i does not work inside the ajax call. i的值在ajax调用中不起作用。 I am trying to pass the value of i so that it can attach the element to the proper div. 我试图传递i的值,以便它可以将元素附加到正确的div。 Can someone help me out? 有人可以帮我吗?

Ok. 好。 This works but would really love if someone can explain it! 这有效但如果有人可以解释它真的很喜欢! I broke my head over this so posting it here just in case someone needs it. 我突然想到这个,所以在这里张贴以防万一有人需要它。

for(i = 0; i < data.length; i++)
   fetchItem(i)

fetchItem = function(i) {
    var query = base_url + data[i]; 
    $.ajax({ 
        url: query, 
        type: 'GET', 
        dataType: 'jsonp', 
        timeout: 2000, 
        error: function() { self.html("Network Error"); }, 
        success: function(json) { 
            $("#li" + i).html("<img src='" + json.result.list[0].url + "' />") 
        } 
    }); 
}

Have you tried making ia global variable: 你有没有尝试过制作全局变量:

Change: 更改:

for(var i = 0; i < data.length; i++) { 
    var query = base_url + data[i]; 
    $.ajax({ 
        url: query, 
        type: 'GET', 
        dataType: 'jsonp', 
        timeout: 2000, 
        error: function() { self.html("Network Error"); }, 
        success: function(json) { 
            $("#li" + i).html("<img src='" + json.result.list[0].url + "' />") 
        } 
    }); 
} 

To this: 对此:

for(i = 0; i < data.length; i++) { 
    var query = base_url + data[i]; 
    $.ajax({ 
        url: query, 
        type: 'GET', 
        dataType: 'jsonp', 
        timeout: 2000, 
        error: function() { self.html("Network Error"); }, 
        success: function(json) { 
            $("#li" + i).html("<img src='" + json.result.list[0].url + "' />") 
        } 
    }); 
} 

Also I agree with Nick. 我也同意尼克。 You can do the looping first then send the array (JSON) serverside to be processed. 您可以先进行循环,然后发送要处理的数组(JSON)服务器端。 This way the application will be much more responsive. 这样,应用程序将更具响应性。

I think your problem has to do with scope. 我认为你的问题与范围有关。

You should wrap the what's in the for loop into a function 你应该把for循环中的内容包装成一个函数

for(var i = 0; i < data.length; i++) {
   doAjax(i);
}

function doAjax(i) {
 var query = base_url + data[i];
    $.ajax({
        url: query,
        type: 'GET',
        dataType: 'jsonp',
        timeout: 2000,
        error: function() { self.html("Network Error"); },
        success: function(json) {
            $("#li" + i).html("<img src='" + json.result.list[0].url + "' />")
        }
    });
}

The i value for the closure function of success is correctly bound only when it is not in a for loop. success闭包函数的i值只有在不在for循环中时才能正确绑定。

That should work. 这应该工作。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM