如何使用JavaScript与Web服务器进行交互 [英] How to interact with Web server using JavaScript

查看:75
本文介绍了如何使用JavaScript与Web服务器进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AppWeb开源Web服务器开发交互式聊天应用程序.
我需要某种机制来使Web服务器能够将更新的消息发送到客户端,以便在远程用户发送消息时,这些消息将在客户端自动更新.

I am trying to develop an Interactive chat application using AppWeb open source web Server.
I need to have some machanism that will enable Web server to send updated messages to client, so that when remote usre sends messages that will get updated automaticaly at the client end.

有一些方法可以使用HTML5 Web套接字和服务器发送的事件来完成此操作.
但是我们只需要用HTML和JavaScript而不是HTML5来实现它.

There are some methods to do this using HTML5 Web Sockets and Server sent events.
But we need to implement it in HTML and JavaScript only not HTML5.

因此,我需要一些合并机制,以便为新事件继续合并我的Web服务器.

So I need some pooling machanism that will keep pooling my Web Server for New events.

所以我应该如何使用Sockets用Javascript编写缓冲机制.
应该如何在服务器端实现它?

So how should I write pooling machanism in Javascript using Sockets.
How it should be implemented at server end?

谢谢!

推荐答案

已经有一些示例了...根据服务器端,您可以使用

there are already some examples out there... depending on the server-side, you could go for java-hello-world or php-hello-world or ...

如果您不能使用 websocket ,则您必须可以采用旧方法,通过 window.setInterval 创建一个间隔,并使用例如从服务器中提取数据. $.ajax().我不知道双向连接(websocket)的其他替代方法... 参见

if you can't use websocket, you have to can go the old way, create an interval by window.setInterval and pull data from the server with eg. $.ajax(). i don't know any other alternative to bidirectional connection (websocket)... see kayahrs answer

根据您的要求:
$.ajax() xhr .基本上,它将向服务器触发一个异步请求,该请求将返回xml或json或text或...(无论如何).当此请求返回时,将触发受支持的eventHandler,您可以对响应做出反应.您也可以使用普通的xhr,但是处理原始的xhr有点尴尬.
jQuery支持 $.ajax()的一些速记重载,例如. $.getJSON() $.get(),...

as you've asked for it:
$.ajax() is the jQuery way to do xhr. basically, it fires an asynchronous request to the server, which returns xml or json or text or ... (whatever). when this request comes back, the supported eventHandler gets fired and you can react to the response. you could also use the plain xhr, but it's a bit awkward to handle the original xhr.
jQuery supports some shorthand overloads for $.ajax(), eg. $.getJSON(), $.get(), ...

示例实现:

$.get("test.cgi", function(data){
    alert("Data Loaded: " + data);
});

这篇关于如何使用JavaScript与Web服务器进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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