gpt4 book ai didi

Loop through URLS and use ajax request each time(遍历URL并每次使用AJAX请求)

转载 作者:bug小助手 更新时间:2023-10-27 20:32:45 26 4
gpt4 key购买 nike



I'm a beginner in JQuery and I'm trying to use an ajax request inside a for loop.
My ultimate goal would be to iterate through an array of urls, to get the content of each page and search for divs with a specific class, then put the divs' contents in an array. after all pages have been explored i would then count the number of occurrences in the array for each value.

我是JQuery的初学者,我正在尝试在for循环中使用AJAX请求。我的最终目标是遍历URL数组,获取每个页面的内容并搜索具有特定类的div,然后将div的内容放入数组中。在浏览完所有页面之后,我将计算每个值在数组中出现的次数。


For now I'm just trying to get the ajax request to work:

现在,我只想让AJAX请求正常工作:


$(document).ready(function(){
var url = 'https://peekaboo-rpg.forumactif.com';
var topic = '/t6203-';
var sectionUrl = [
'','p10-','p20-';

for(var i=0; i<=sectionUrl.length; i++){
var keySearch = url+topic+sectionUrl[i];

$.ajax({
url:keySearch,
success:function(data){
console.log(keySearch);
}
});
}
});

This code would simply return the url for each page in the console and actually it returns "undefined" for the sectionUrl part; so when I try to actually fetch my data likes this:

这段代码只返回控制台中每个页面的URL,实际上它为sectionUrl部分返回“unfined”;因此,当我尝试实际获取数据时,如下所示:


lesgens = [].slice.call($(".pab_sujpseudo a span strong", data));
lesnoms = lesgens.map(function(elem) { return elem.innerHTML; });
console.log(lesnoms);

it always returns the same values, those from the first item in my array.

它总是返回相同的值,即数组中第一个元素的值。


更多回答

you want to make multiple AJAX requests inside a loop and process the data from each request. The issue you're facing with your code is related to the variable keySearch inside the AJAX success callback.

您希望在一个循环中发出多个AJAX请求,并处理来自每个请求的数据。您的代码面临的问题与AJAX Success回调中的变量keySearch有关。

The variable keySearch is being overwritten in each iteration of the loop, and when the AJAX request completes and executes the success callback, it references the final value of keySearch.

变量keySearch在循环的每次迭代中都会被覆盖,当AJAX请求完成并执行Success回调时,它会引用keySearch的最终值。

Did I understand correctly?

我的理解正确吗?

Yes I want multiple ajax requests, but actually from what i could gather the keySearch variable isn't being overwritten, it just is undefined for some reason?

是的,我想要多个AJAX请求,但实际上从我收集到的信息来看,keySearch变量没有被覆盖,只是出于某种原因没有定义?

try using let for variable declaration. let keySearch = url+topic+sectionUrl[i];

尝试使用LET进行变量声明。让keySearch=url+主题+sectionUrl[i];

优秀答案推荐

$(document).ready(function(){
var url = 'https://peekaboo-rpg.forumactif.com';
var topic = '/t6203-';
var sectionUrl = ['', 'p10-', 'p20-'];

function fetchData(keySearch) {
$.ajax({
url: keySearch,
success: function(data) {
var lesGens = [].slice.call($(".pab_sujpseudo a span strong", data));
var lesNoms = lesGens.map(function(elem) { return elem.innerHTML; });
console.log(lesNoms);
}
});
}

for (var i = 0; i < sectionUrl.length; i++) {
var keySearch = url + topic + sectionUrl[i];
fetchData(keySearch);
}
});

By declaring lesGens and lesNoms inside the success callback function using var, you ensure that each AJAX request has its own separate variables for storing the data, and they won't overwrite each other.

通过使用var在Success回调函数中声明lesGens和lesNom,您可以确保每个AJAX请求都有自己的单独变量来存储数据,并且它们不会相互覆盖。


更多回答

Thank you for your solution, it works so far! thanks for your advice as well, i have about 80 urls to fetch so that's quite alot... however, for the use of the script i have i think it's ok like this, since i should only be running it for myself once a month... I'm still interested in making the script more efficient tho if you ever have time to explain :)

感谢您的解决方案,到目前为止,它是有效的!也感谢你的建议,我有大约80个网址,所以这是相当多…然而,对于我拥有的脚本的使用,我认为这样还可以,因为我应该每个月只为自己运行一次…不过,如果您有时间解释一下,我仍然有兴趣让脚本更高效:)

Actually sorry to bother you again, it seems keySearch does have the right value for each loop, however data remains always the same?

实际上很抱歉再次打扰您,似乎KeySearch对于每个循环都有正确的值,但是数据总是相同的吗?

I see the issue now. I will edit my response.

我现在明白问题所在了。我将编辑我的回复。

I'm still experimenting the same problem with the new solution, I'm sorry :( Like, even if i just do console.log(data), it always returns the same value for data, although it seems to loop correctly through the urls, and I really don't understand how that could be (EDIT: and the value seems to be associated to the url without the sectionUrl part)

我仍然在用新的解决方案试验同样的问题,很抱歉:(例如,即使我只做sole.log(Data),它也总是返回相同的数据值,尽管它似乎正确地循环遍历了URL,我真的不明白这是怎么回事(EDIT:并且该值似乎与没有sectionUrl部分的URL相关联)

when i try solution, result seems as below: jQuery element Selector This is p tag This is span tag This is div tag

当我尝试解决方案时,结果似乎如下:jQuery元素选择器这是p标记这是span标记这是div标记

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