如何从本机 Android 2.1 浏览器使用 javaScript 检查与服务器的连接?
我正在构建一个网络应用程序,需要在在线或离线时无缝工作。为此,需要能够检查我与服务器的连接。
主要支持的设备是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太明白你的情况,但听起来你应该使用...
http://api. jquery.com/jQuery.ajax/
async : false 是使用 test.html 或任何其他简单文件(基本上我们' (只是尝试连接。)您可以使用超时来设置一些较短的时间段。
然后看看您是否收到错误或成功...
如果出现错误,请触发一些函数来处理该错误,如果您取回文件,就可以开始了。
I don't exactly understand your situation but it sounds like you should use ...
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 ...
If there is an error, fire some function to handle that, if you get the file back you are good to go.
毕竟它与“同源政策”有关(参见评论)。
使用 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);
}
我在网络应用程序中使用
$.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