body标签内的未定义全局数组 [英] Undefined global array inside body tag

查看:102
本文介绍了body标签内的未定义全局数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我第一次来这个论坛。我希望有人可以帮助我。

我试图从几个不同的结构相同的网页中获取11个值。我将网页列表保存为全局数组。我试图一次访问一个站点,并使用for循环获取数据,将每个值保存到一个声明为全局变量的数组中。我试图从主体的html访问该网站,并将每个值放入一个表的行,然后用值清空数组并启动for循环访问下一个站点。

我测试过看到页面正在被正确读取,并且值数组中有正确的数据。据我所知,一切正常。然而,当我试图将它写入我的桌子时,它会显示为一个充满单词undefined的表格。


我希望这一切都清楚。出于隐私原因,我从代码中删除了一些细节。任何建议表示赞赏。


谢谢

Greetings,

My first time on this forum. I hope someone can help me out.
I am trying to get 11 values from several different identically structured webpages. I have the list of webpages saved as a global array. I am trying to access the sites one at a time and acquire the data using a for loop saving each value into an array that was declared as a global variable. I am trying to access the site from Body of the html and place each value into a row of a table and then empty the array with the values and start the for loop over accessing the next site.
I tested to see that the page is being read properly and that the value array has the correct data in it. As far as I can tell that all works fine. However when trying to write it to my table it comes out as a table full of the word "undefined."

I hope this is all clear. I removed some of the specifics from the code for privacy reasons. Any advice is appreciated.

Thanks

展开 | 选择 | Wrap | 行号

推荐答案

嗨...


因为ajax-call是异步的,你在初始化调用之后就没有在数组中找到检索到的值...所以你应该从您的alertContent方法中编写它,该方法在响应可以使用时调用。您应该注意,使用您当前的代码,警报发布在您的document.write之后?


亲切的问候
hi ...

since the ajax-call is async you don''t have the retrieved values in your array right after initializing the call ... so you should write it from your alertContent method, that is called when the response is ready to use. you should note that with your current code the alert comes after your document.write?

kind regards


感谢您的回复。 />

如果我将body标签中的代码块放入我的alertContents函数中,我会以递归方式调用makeRequest吗?一个makeRequest函数会在下一个被放入堆栈之前调用end(如果这是正确的术语)?这似乎可能是缓慢或内存效率低下,如果这是真正发生的事情。


我也必须通过在外面第一次调用函数来启动它。


我希望我能正确理解你。
Thank you for your response.

If I put the block of code I have inside the body tag into my alertContents function would I be calling makeRequest recursively? Would one makeRequest function call end before the next got put onto the stack(if that''s the correct term)? This seems like it might be slow or memory inefficient if that is what would really happen.

I would also have to initiate this by calling the function the first time somewhere outside.

I hope I am understanding you correctly.


nope ...它不会再递称调用...你只需要确保在您想要使用它之前得到了响应! ...所以在你的情况下你填充值数组...但现在你需要一个机制,当你准备好所有的值或者你可以(并且可能应该)避免文档写入和更新时调用文档更新函数每当响应返回时,innerHTML就没有内存效率......它只是一个异步调用,我们必须处理异步数据流:


当您发送XMLHttpRequest时,您告诉它onreadystatechange是要做的事情,通常当readystate为4且请求状态为200时,您就有了获得响应的时刻。你必须以一种你可以依赖响应的方式将这些时刻与你的代码联系在一起......你不应该使用超时或等待什么......因为我们总是知道响应何时回来:)。 ..另一种方式是进行同步调用......但这很难看,你几乎消除了ajax的所有优点...


亲切的问候
nope ... it doesnt''t call recursivly then ... you only have to assure that you got the response before you want to use it! ... so in your case you fill the values array ... but now you would need a mechanism that invokes the document-update function when you have all your values ready or you could (and probably should) avoid document-write and update the innerHTML everytime a response returns ... there is nothing memory-inefficient with that ... it is just an async call and we have to deal with the async data-flow:

when you send a XMLHttpRequest you tell it that onreadystatechange is something to do and usually when the readystate is 4 and the request-status is 200 you have the moment where you have your response. you have to chain that moments with your code in a way that you can rely on the response ... you shouldn''t use timeouts or whatever to wait for something ... since we always know when the response is back :) ... another way would be to make a sync call ... but that is ugly and you loose nearly all advantages of ajax ...

kind regards


这篇关于body标签内的未定义全局数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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