如何从本机 Android 2.1 浏览器使用 javaScript 检查与服务器的连接?

发布于 2024-10-06 05:33:49 字数 371 浏览 0 评论 0原文

我正在构建一个网络应用程序,需要在在线或离线时无缝工作。为此,需要能够检查我与服务器的连接

主要支持的设备是 iphone3+4、ipad、带有 chrome 的上网本和 android 2.1+。

navigator.onLine 不正是我正在寻找的。这是不可靠的,仅检查您是否具有连接,而不检查您是否连接到特定服务器。

我尝试使用 jQuery.getJSON 连接到我的服务器上的 ping Web 服务。这在上网本上的 Chrome 中运行良好,但本机 Android 浏览器返回 null。 iPod touch 根本不做任何事情。

我可以在这方面需要一些帮助..

I am building a web application that needs to be working seamless when being online or off line. For this need to be able to check my connection with the server.

The main devices that are to be supported are iphone3+4, ipad, a netbook with chrome and android 2.1+.

The navigator.onLine is not exactly what I am looking for. This is unreliable and only checks if you have connectivity, not if you are connected to a specific server.

I tried jQuery.getJSON to connect to a ping web-service on my server. This works fine in chrome on the netbook but the native android browser returns null. Ipod touch doesn't do anything at all.

I could use some help with this..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人哭 2024-10-13 05:33:49

我不太明白你的情况,但听起来你应该使用...

$.ajax({
 url: "test.html",
 cache: false,
 async : false,
 error: function(XMLHttpRequest, textStatus, errorThrown) {
        // some error code.
        },
 success: function(html){
          // do something.
        }
});

http://api. jquery.com/jQuery.ajax/

async : false 是使用 test.html 或任何其他简单文件(基本上我们' (只是尝试连接。)您可以使用超时来设置一些较短的时间段。

然后看看您是否收到错误成功...

错误(XMLHttpRequest,textStatus,errorThrown)

请求时调用的函数
失败。该函数传递了三个
参数: XMLHttpRequest 对象,
描述错误类型的字符串
发生的情况和可选的
异常对象(如果发生)。
第二个的可能值
参数(除了空)是“超时”,
“错误”、“未修改”和
“解析器错误”。这是一个阿贾克斯
Event.This 处理程序未被调用
JSONP 请求,因为它们不
使用 XMLHttpRequest。

如果出现错误,请触发一些函数来处理该错误,如果您取回文件,就可以开始了。

I don't exactly understand your situation but it sounds like you should use ...

$.ajax({
 url: "test.html",
 cache: false,
 async : false,
 error: function(XMLHttpRequest, textStatus, errorThrown) {
        // some error code.
        },
 success: function(html){
          // do something.
        }
});

http://api.jquery.com/jQuery.ajax/

async : false is to "block" the request, using test.html or any other simple file (basically we're just trying to connect.) You could use the timeout to set some short time period.

Then see if you get get an error or success ...

error(XMLHttpRequest, textStatus, errorThrown)

A function to be called if the request
fails. The function is passed three
arguments: The XMLHttpRequest object,
a string describing the type of error
that occurred and an optional
exception object, if one occurred.
Possible values for the second
argument (besides null) are "timeout",
"error", "notmodified" and
"parsererror". This is an Ajax
Event.This handler is not called for
JSONP requests, because they do not
use an XMLHttpRequest.

If there is an error, fire some function to handle that, if you get the file back you are good to go.

新一帅帅 2024-10-13 05:33:49

毕竟它与“同源政策”有关(参见评论)。
使用 JSONP 解决了这个问题

奇怪的是,看起来我正在处理同一个域,但是代码在这里......

客户端:
jQuery.getJSON("url?callback=?", function(data) {
//做某事
});

服务器端:
@RequestMapping(value="url", method=RequestMethod.GET)
public void ping(HttpServletRequest请求,HttpServletResponse响应)
抛出 IOException {
字符串输出 = request.getParameter("callback") + "({0:1});";
response.setContentType(“文本/javascript”);
response.getWriter().write(输出);
}

It after all had to do with the 'same origin policy' (see comments).
This was resolved by using JSONP.

The strange thing is that it looks like I am dealing with the same domain, but oke here the code...

CLIENT-SIDE:
jQuery.getJSON("url?callback=?", function(data) {
//Do something
});

SERVER-SIDE:
@RequestMapping(value="url", method=RequestMethod.GET)
public void ping(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String output = request.getParameter("callback") + "({0:1});";
response.setContentType("text/javascript");
response.getWriter().write(output);
}

默嘫て 2024-10-13 05:33:49

我在网络应用程序中使用 $.getJSON 时遇到了类似的问题。除了 Android 之外,它在许多浏览器上都能正常运行。事实上,我的应用程序应该离线工作,然后我使用一个清单,其中 JSON 文件被声明为 CACHE。对于初始加载,它运行良好,但对于下一页加载,应用程序在加载 JSON 文件时会被阻止。

我怀疑脱机缓存被视为初始服务器之外的另一个来源,并且该进程被阻止。

希望有帮助

I had a similar issue using $.getJSON in a web app. It works well from many browsers except on Android. In fact, my app is supposed to work offline and then I use a manifest where the JSON file is declared as CACHE. For the initial load, it works well but for the next pages load the app is blocked when loading the JSON file.

I suspect that the offline cache is perceived as another origin than the initial server and the process is blocked.

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文