我是在我的系统上不使用服务器运行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页面。我在Google上搜索了很多关于如何在
jquery.ajax
中本地加载数据而不使用服务器的内容,但没有找到任何好的解决方案,因此我认为如果它驻留在Stack overflow上,对其他人也是有好处的。
do_the_click(this.href)
而不是带单引号的形式。但由于文件名中有空格,请求可能仍然会失败,因此你可能需要去掉空格。 - Jarrett Widman