重复XMLHttpRequest GET并显示结果 [英] repeated XMLHttpRequest GETs and displaying result

查看:131
本文介绍了重复XMLHttpRequest GET并显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对javascript有点新鲜 - 下面将会很明显。


我正在使用XMLHttpRequest从服务器(django)获取一些数据,

,它适用于单个事件。但我的最终结果需要

才能成为一系列数据传输。我想在

及以上请求数据,直到数据为触发停止的某个值,这将是一个有趣的第一次尝试。为了继续前进,我在for-loop中包装了我的初始

单个数据请求,看看我是否可以进行多次通话。


这是'我的javascript东西......

I''m a bit new to javascript - as will be obvious below.

I''m using an XMLHttpRequest to get a bit of data from server (django),
and it works nicely for single events. But my eventual outcome needs
to be a series of data transmissions. I figured requesting data over
and over until the data is some value that triggers the stop would be
an interesting first attempt. To move forward, I wrapped my initial
single data request in a for-loop to see if I could do multiple calls.

Here''s my javascript stuff...

展开 | 选择 | Wrap | 行号

推荐答案

6月25日,2:44 *下午,RossGK< ros ... @ gmail.comwrote:
On Jun 25, 2:44*pm, RossGK <ros...@gmail.comwrote:

我对javascript有点新鲜 - 下面将会很明显。


我正在使用XMLHttpRequest从服务器(django)获取一些数据,

它运行良好f或单一事件。 *但我的最终结果需要

才能成为一系列数据传输。 *我想在

及以上请求数据,直到数据触发停止的某个值将是有趣的第一次尝试。 *为了继续前进,我在for-loop中包装了我的初始

单个数据请求,看看我是否可以进行多次通话。


这里''我的javascript东西...


* * * * for(i = 0; i< = 5; i ++){

* * * * * * * * xmlHttp.onreadystatechange = function()


* * * * * * * * {if(xmlHttp.readyState == 4)

* * * * * * * * * * * * {document.myForm.myvar.value = xmlHttp.responseText;

* * * * * * * * * * * *}

* * * * * * * *}

* * * * * * * * xmlHttp.open(" GET"," / ajax_data",true);

* * * * * * * * xmlHttp.send(null);


* * * * * * * * // alert(来这里)

* * * * * * * * dopause(500); * * * * //等待500毫秒

* * * * *}


我注意到的是,该警报线被注释掉,没有

似乎发生了 - 我没看到数据收到。 *我希望看到

闪存5次(服务器发送一个随机数)


当我发出警报时,警报确实会弹出显示数据




警报消息是否会导致我需要模拟的窗口刷新

警报不存在。 *我需要做些什么来让它循环

通过get 5次实际显示出现的内容,没有

使用警报弹出!?


感谢您的任何建议
I''m a bit new to javascript - as will be obvious below.

I''m using an XMLHttpRequest to get a bit of data from server (django),
and it works nicely for single events. *But my eventual outcome needs
to be a series of data transmissions. *I figured requesting data over
and over until the data is some value that triggers the stop would be
an interesting first attempt. *To move forward, I wrapped my initial
single data request in a for-loop to see if I could do multiple calls.

Here''s my javascript stuff...

* * * * for (i=0;i<=5;i++) {
* * * * * * * * xmlHttp.onreadystatechange=function()

* * * * * * * * { if (xmlHttp.readyState==4)
* * * * * * * * * * * * { document.myForm.myvar.value=xmlHttp.responseText;
* * * * * * * * * * * * }
* * * * * * * * }
* * * * * * * * xmlHttp.open("GET","/ajax_data",true);
* * * * * * * * xmlHttp.send(null);

* * * * * * * * //alert("got here")
* * * * * * * * dopause(500); * * * *//wait 500ms
* * * * * }

What I''ve noticed is that with that alert line commented out, nothing
seems to happen -ie I don''t see the data receive. *I expected to see
it flash 5 times (the server sends a random number)

When I put the alert in, the alert does in fact pop up, and the data
gets displayed.

Does the alert message cause a window refresh that I need to emulate
when the alert is not there. *What do I need to do to make it cycle
through the get 5 times and actually show me what comes up, WITHOUT
using an alert pop!?

Thanks for any suggestions



希望我可以测试一下,但我没有资源。我假设

myvar是表单中的文本字段或文本区域?尝试在本节中附上

响应文本 - 不只是设置它(不是作为一个

永久解决方案,请注意,但只是为了调试)。可能是用空响应覆盖了


Wish I could test this, but I don''t have the resources. I''m assuming
the myvar is a text field or text area in your form? Try appending
the responsetext to this section - not just setting it (not as a
permanent solution, mind you, but just to debug). It may be being
overwritten with a null response.



希望我能测试一下,但我没有资源。我假设

myvar是表单中的文本字段或文本区域?
Wish I could test this, but I don''t have the resources. I''m assuming
the myvar is a text field or text area in your form?



是的,文本在下面的表格中分配给myvar ...


< form name =" myForm">

Stuff:< input type =" text"的onkeyup = QUOT; getServerData();"命名= QUOT;用户名" /


Yes, the text gets assigned to myvar in the form below...

<form name="myForm">
Stuff: <input type="text" onkeyup="getServerData();" name="username" /


>
>



响应:< input type =" text"名称= QUOT; MYVAR" />

< / form>


....正如我所说,在单个事件中或使用警报

pop。

Response: <input type="text" name="myvar" />
</form>

.... and as I say works nicely in a single event, or with the alert
pop.


尝试将responsetext附加到此部分 - 不仅仅是设置它(而不是作为

永久解决方案,请注意,但只是为了调试)。它可能是用空响应覆盖了


Try appending the responsetext to this section - not just setting it (not as a
permanent solution, mind you, but just to debug). It may be being
overwritten with a null response.



不确定你的意思......你的意思是某种打印

声明? (对不起,我的javascript漏洞正在显示)....

Not sure what you mean... do you mean like some sort of print
statement? (sorry my javascript weakness is showing)....


6月25日,3:44 * pm,RossGK< ros ... @ gmail.comwrote:
On Jun 25, 3:44*pm, RossGK <ros...@gmail.comwrote:

希望我可以测试一下,但我没有资源。 *我假设

myvar是表单中的文本字段或文本区域?
Wish I could test this, but I don''t have the resources. *I''m assuming
the myvar is a text field or text area in your form?



是的,文本在下面的表格中分配给myvar ...


< form name =" myForm">

* * * * Stuff:< input type =" text"的onkeyup = QUOT; getServerData();"命名= QUOT;用户名" /


* * * *响应:< input type =" text"名称= QUOT; MYVAR" />

< / form>


......正如我所说,在单个活动中或使用警报时效果很好

pop。


Yes, the text gets assigned to myvar in the form below...

<form name="myForm">
* * * * Stuff: <input type="text" onkeyup="getServerData();" name="username" /

* * * * Response: <input type="text" name="myvar" />
</form>

... and as I say works nicely in a single event, or with the alert
pop.


尝试将响应文本附加到此部分 - 而不仅仅是设置它(而不是作为

永久解决方案,介意你,但只是为了调试)。 *可能是用空响应覆盖了


Try appending the responsetext to this section - not just setting it (not as a
permanent solution, mind you, but just to debug). *It may be being
overwritten with a null response.



不确定你的意思......你的意思是某种打印

声明? *(对不起,我的javascript弱点显示)....


Not sure what you mean... do you mean like some sort of print
statement? *(sorry my javascript weakness is showing)....



尝试document.myForm.myvar.value + = xmlHttp.responseText;


而不是


document.myForm.myvar.value = xmlHttp.responseText;

Try document.myForm.myvar.value+=xmlHttp.responseText;

instead of

document.myForm.myvar.value=xmlHttp.responseText;


这篇关于重复XMLHttpRequest GET并显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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