我在我的系统上运行没有服务器的 ajax,我用它创建了一个 index.html。
JavaScript 函数:
function do_the_click(url)
{
alert('inside this method do_the_click');
$.ajax({
async: false,
url : url,
contentType: "text/html",
type : "GET",
dataType : "text/html",
success: function(data){
alert('data');
}});
}
我的 HTML 正文内容是:
<a href="Java Collections.html" class="button" id="javacollections" onclick="do_the_click('this.href');"/>Java Collections</a>
我能够在警告窗口中获取此消息 在此方法 do_the_click
中,但我无法在警告窗口中获取数据,因此无法获取index.html中的Java Collections.html页面,
我在谷歌上搜索了很多。如何在不使用 jquery.ajax
中的服务器的情况下在本地加载数据,但我没有找到任何好的解决方案,所以我认为如果它驻留在 Stack overflow 上,它对其他人也有好处。
最佳答案
你不能做这样的事。如果您没有服务器,则无法发送 ajax,这将是一个跨浏览器问题。正如其他人所说,ajax 不能使用 file://
协议(protocol),您需要一个服务器来进行 ajax 支持的 http://
调用。
关于javascript - 如何在没有 Web 服务器的情况下运行 ajax 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829481/