0
這段代碼應該循環遍歷幾個網頁,並找到每個頁面上的所有鏈接,並在控制檯中列出它們,但它似乎只列出最後一個網頁(fakeURL)中的鏈接。 COM/735)。For循環中的XMLHttpRequest
我怎麼能得到console.log(url);
工作的每一次迭代,而不是最後一個(這是當我= 735)?
for(i = 0; i <= 735; i += 15)
{
var xhrs = new XMLHttpRequest();
xhrs.open("get", 'http://fakeURL.com/' + i, true);
xhrs.onreadystatechange = function()
{
if (xhrs.readyState == 4)
{
$(xhrs.responseText).find('a').each(function()
{
var url = $(this).attr('href');
console.log(url);
});
}
}
xhrs.send();
}
你應該看到:http://stackoverflow.com/questions/26100329/xhr-response-with-lo--op-not-working/26100409#26100409 順便說一句,'$'似乎是jQuery,所以爲什麼你不使用'$ .ajax()'? – laruiss 2014-10-05 00:25:27
你的代碼會啓動大量的ajax調用,然後他們都會在稍後完成。你的問題到底是什麼? – jfriend00 2014-10-05 00:59:01
@ jfriend00我需要'console.log(url)'在每次迭代期間發生。現在它只發生在最後的迭代。 – Farad 2014-10-05 01:37:23