gpt4 book ai didi

jquery - 如何使用 native Android 2.1 浏览器中的 javaScript 检查与服务器的连接?

转载 作者:搜寻专家 更新时间:2023-11-01 07:40:32 25 4
gpt4 key购买 nike

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

主要支持的设备有iphone3+4、ipad、带chrome的netbook和android 2.1+。

navigator.onLine 并不是我想要的。这是不可靠的,只检查您是否有连接,而不检查您是否连接到特定服务器。

我尝试使用 jQuery.getJSON 连接到我服务器上的 ping 网络服务。这在上网本上的 chrome 中工作正常,但 native android 浏览器返回 null。 Ipod touch 什么都不做。

我需要一些帮助......

最佳答案

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

$.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 或任何其他简单文件(基本上我们只是尝试连接。)你可以使用超时设置一些较短的时间段。

然后看看你是否得到一个错误或者成功 ...

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.

如果有错误,触发一些函数来处理它,如果你拿回文件,你就可以继续了。

关于jquery - 如何使用 native Android 2.1 浏览器中的 javaScript 检查与服务器的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4373266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com