简体   繁体   English

带有JQ的Ajax请求在Chrome中显示错误:“无法加载资源”

[英]Ajax request with JQ gives an error in Chrome: “Failed to load resource”

My idea is to ping the server to know if there is an internet connection, if there is, it will navigate to MyPage.html, if not it will show an alert. 我的想法是对服务器执行ping操作,以了解是否存在Internet连接,如果存在,它将导航至MyPage.html,否则将显示警报。

Nevertheless, it seems that Chrome does not download the ping file to my PC and I get the error "Failed to load resource: net". 但是,Chrome似乎没有将ping文件下载到我的PC上,并且出现错误“无法加载资源:网络”。

Why is this failing? 为什么会失败? This is just a plain text file. 这只是纯文本文件。

(The file exists and I can see its content using its URL path perfectly. The file is located in the same directory as the script, so there are not cross domains issues). (该文件存在,并且可以完美地使用其URL路径查看其内容。该文件与脚本位于同一目录中,因此没有跨域问题)。

Is there something related to my server? 与我的服务器有关吗?

Thanks 谢谢

function ShowURL(){

$.ajax({
    url:'ping.txt',
    DataType:'text',
    cache: false,
    success: function (){
        window.location.href  = 'mypage.html';
    },
    error: function () {
        alert('Sorry, Internet conecction is required to perform this task');
    }
});

}

After hours and hours of try an Error I found the Answer: Dedicated for those guys who are facing the same problem: 经过数小时的尝试后,我发现了答案:专为那些面临相同问题的人:

In my case this was not a common Web Page, it was an Offline Web Page with a Manifest File. 就我而言,这不是常见的网页,而是带有清单文件的脱机网页。

Simply in the section "NETWORK" of the manifest file include the file "ping.txt" 在清单文件的“网络”部分中,只需包含文件“ ping.txt”

Tha's all folks! 他是所有人!

You should use: contentType:'text/plain' instead of DataType:'text' 您应该使用: contentType:'text/plain'而不是DataType:'text'

You could just ask the browser if it was online through: navigator.onLine it returns a boolean. 您可以通过以下方式询问浏览器是否在线: navigator.onLine它返回一个布尔值。 This doesn't require your server to be online for the client to be online :) 这不需要您的服务器在线即可使客户端在线:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM