I have a page that uses Javascript in which I need to have a function that listens for data to be received from the server. Basically, anytime it hits the server, I need the function to fire.
我有一个使用Javascript的页面,我需要有一个函数来监听从服务器接收的数据。基本上,无论什么时候它到达服务器,我都需要触发功能。
This seems like it would be pretty straightforward - but so far nothing I've tried works.
这看起来很简单 - 但到目前为止我没有尝试过任何工作。
I know I have the basic set-up correct - because I can get it to listen for mouse up, mouse down, and load - but nothing for when it sends or receives data from the server.
我知道我的基本设置是正确的 - 因为我可以让它听取鼠标,鼠标按下和加载 - 但是当它从服务器发送或接收数据时没有任何内容。
For example, this works...
例如,这有效......
document.attachEvent('onmouseup', function(event) {
alert("mouse released!");
});
So does this...
这个......
window.attachEvent('onload', function(event) {
alert("page loaded!");
});
However, none of these do...
但是,这些都不是......
document.attachEvent('onreceive', function(event) {
alert("data received!");
});
window.attachEvent('onreceive', function(event) {
alert("data received!");
});
document.attachEvent('onpost', function(event) {
alert("data posted!");
});
window.attachEvent('onpost', function(event) {
alert("data posted!");
});
document.attachEvent('onmessage', function(event) {
alert("data posted!");
});
window.attachEvent('onmessage', function(event) {
alert("data posted!");
});
Any suggestions?
Thanks in advance!
提前致谢!
1 个解决方案
#1
1
I think there are no way, as per addEventListener specification
:
我认为根据addEventListener规范没有办法:
The EventTarget.addEventListener() method registers the specified listener on the EventTarget it's called on. The event target may be an Element in a document, the Document itself, a Window, or any other object that supports events (such as XMLHttpRequest).
EventTarget.addEventListener()方法在调用它的EventTarget上注册指定的侦听器。事件目标可以是文档中的元素,文档本身,窗口或支持事件的任何其他对象(例如XMLHttpRequest)。
And indeed, you can check it by searching on the event reference list here.
实际上,您可以通过在此处搜索事件参考列表来检查它。
Apparently, you'll need to:
显然,你需要:
- (a.) call the handler functions manually before and after every ajax call you make; or
- (b.) use a javascript framework to make ajax calls (so it does the manual work for you)
(a。)在你做的每个ajax调用之前和之后手动调用处理函数;要么
(b。)使用javascript框架进行ajax调用(所以它为你做了手动工作)
In case you choose (b.):
如果你选择(b。):
-
There are a lot of frameworks around there, but I'm suggesting here jQuery or AngularJs.
那里有很多框架,但我在这里建议使用jQuery或AngularJs。
- You can find interesting free courses here¹ if you need some introduction to them
你可以在这里找到有趣的免费课程¹如果你需要一些介绍
-
If you choose jQuery:
如果你选择jQuery:
- use
ajaxSuccess
for handling all responses - set a global
beforeSend
onajaxSetup
for handling all requests
使用ajaxSuccess来处理所有响应
在ajaxSetup上设置一个全局的beforeSend来处理所有请求
- use
-
If you choose AngularJS:
如果您选择AngularJS:
- use Interceptors when globally handling responses and requests
- there's another nice angular interceptor tutorial
在全局处理响应和请求时使用拦截器
还有另一个很棒的角度拦截器教程
¹ I do not work for the linked course site. I posted it here as a personal suggestion.
¹我不适用于链接课程网站。我在这里发布它作为个人建议。
#1
1
I think there are no way, as per addEventListener specification
:
我认为根据addEventListener规范没有办法:
The EventTarget.addEventListener() method registers the specified listener on the EventTarget it's called on. The event target may be an Element in a document, the Document itself, a Window, or any other object that supports events (such as XMLHttpRequest).
EventTarget.addEventListener()方法在调用它的EventTarget上注册指定的侦听器。事件目标可以是文档中的元素,文档本身,窗口或支持事件的任何其他对象(例如XMLHttpRequest)。
And indeed, you can check it by searching on the event reference list here.
实际上,您可以通过在此处搜索事件参考列表来检查它。
Apparently, you'll need to:
显然,你需要:
- (a.) call the handler functions manually before and after every ajax call you make; or
- (b.) use a javascript framework to make ajax calls (so it does the manual work for you)
(a。)在你做的每个ajax调用之前和之后手动调用处理函数;要么
(b。)使用javascript框架进行ajax调用(所以它为你做了手动工作)
In case you choose (b.):
如果你选择(b。):
-
There are a lot of frameworks around there, but I'm suggesting here jQuery or AngularJs.
那里有很多框架,但我在这里建议使用jQuery或AngularJs。
- You can find interesting free courses here¹ if you need some introduction to them
你可以在这里找到有趣的免费课程¹如果你需要一些介绍
-
If you choose jQuery:
如果你选择jQuery:
- use
ajaxSuccess
for handling all responses - set a global
beforeSend
onajaxSetup
for handling all requests
使用ajaxSuccess来处理所有响应
在ajaxSetup上设置一个全局的beforeSend来处理所有请求
- use
-
If you choose AngularJS:
如果您选择AngularJS:
- use Interceptors when globally handling responses and requests
- there's another nice angular interceptor tutorial
在全局处理响应和请求时使用拦截器
还有另一个很棒的角度拦截器教程
¹ I do not work for the linked course site. I posted it here as a personal suggestion.
¹我不适用于链接课程网站。我在这里发布它作为个人建议。