我正在做一些大学考试测试,要求我从对象中的href的url中获取JSON文件。在此之后,我需要动态更改HTML与JSON文件的信息,所以我想我需要使用$ .ajax()或$ .get()方法。我可以让我的代码只用$ .load()方法工作,而其他任何方法什么都不做,也就是.addClass(),而不是其他的方法。 .removeClass()和event.preventDefault()工作正常。
这里是我的代码: JS - >脚本标记之间在页面的开始:
$(document).ready(function()
{
$("nav li a").click(function(event)
{
$("nav li a").not(this).removeClass("active");
$(this).addClass("active");
$("#ris").load($(this).attr('href')); //this work
$.ajax({url: $(this).attr('href'), dataType: "json", //this give the alert
success: function(result)
{$("#ris").html(result);},
error: function(){alert("Error");}
});
$.get($(this).attr('href'), function(json) { //this doesn't work
$("#ris").html(json);
});
$.getJSON($(this).attr('href'), function(json){ //this doesn't work
$("#ris").html(json);});
event.preventDefault();
});
});
HTML:我把唯一的导航项目在这里,#ris是的结束页
<nav>
<li><a href="./Asia.json">Asia</a></li>
<li><a href="./Europa.json">Europa</a></li>
<li><a href="./America.json">America</a></li>
<li><a href="./Africa.json">Africa</a></li>
</nav>
现在我只是把结果放在#ris看它是否工作。但我不明白为什么我不能用任何方法进行工作。
编辑:服务器永远不会收到JSON文件的GET请求。 (服务器日志)
您在开发者控制台中看到什么? – epascarello
http://api.jquery.com/jquery.getjson/ –