为什么这不起作用??jquery ajax替换特定div中的内容
下面是我网站上的代码,但是当我点击这个特别的为什么这不起作用??jquery ajax替换特定div中的内容,jquery,ajax,html,dynamic,Jquery,Ajax,Html,Dynamic,下面是我网站上的代码,但是当我点击这个特别的div中的链接时,它会打开一个新窗口,这是为什么 $(function(){ //bind a click event to the nav links $("#links a").bind("click", function(e){ //keep the links from going to another page by preventing their default behavior e.preventDefault
div
中的链接时,它会打开一个新窗口,这是为什么
$(function(){
//bind a click event to the nav links
$("#links a").bind("click", function(e){
//keep the links from going to another page by preventing their default behavior
e.preventDefault();
//this = link; grab the url
var pageLocation = this.href;
//fire off an ajax request
$.ajax({
url: pageLocation,
//on success, set the html to the responsetext
success: function(data){
$("#biocontent").html(data.responseText);
}
});
});
});
编辑:
我仍然没有弄明白这一点,但这里是我的位置,我的脚本如下
<script type="text/javascript">(function(){
//bind a click event to the nav links
$("#links a").bind("click", function(e){
//keep the links from going to another page by preventing their default behavior
e.preventDefault();
//this = link; grab the url
var pageLocation = $(this).attr("href")
//fire off an ajax request
$.ajax({
url: pageLocation,
//on success, set the html to the responsetext
success: function(data){
$("#biocontent").html(data);
},
dataType : "html"
});
});
(函数(){
//将单击事件绑定到导航链接
$(“#链接a”).bind(“单击”,函数(e){
//通过阻止链接的默认行为,防止链接转到其他页面
e、 预防默认值();
//this=链接;获取url
var pageLocation=$(this.attr(“href”)
//发出ajax请求
$.ajax({
url:pageLocation,
//成功后,将html设置为responsetext
成功:函数(数据){
$(“#biocontent”).html(数据);
},
数据类型:“html”
});
});
}));
下面是我在html
但还是不行,请告诉我你的想法
编辑
所以我把代码的最小部分移到了一个新的文档中,现在它根本不起作用,但我觉得Jquery有问题,所以现在它起作用了,我在
17XMLHttpRequest无法加载file:///Users/semaj4712/Desktop/WME%20Website/testvid.html. 访问控制允许原点不允许原点为null
我不知道这意味着什么,所以一旦我在这里开始工作,我就必须在实际站点上用Jquery解决这个问题。在成功回调中,您的数据对象需要是JSON对象才能包含
responseText
,因此您需要将ajax调用中的dataType
字段设置为'JSON'
。在您当前的情况下,看起来您正在检索html。有关更多信息,请查看上的ajax文档
尝试使用chrome中的javascript控制台
,或firefox中的firebug
查看任何错误。您可能会在页面加载中找到一个
编辑:
您正在处理您的文件系统!由于浏览器强制执行的同源策略,您无法跨文件系统进行ajax调用。这意味着您不能向其他服务器发出ajax请求。为了完成请求,您需要将html页面放在同一个域中。你所做的就像是从www.google.com请求数据,而你的域名是www.mydomain.com。您只能从www.mydomain.com请求数据。最好的方法是在您的机器上设置一个apache服务器。您应该使用您想要获得的指定类型调用ajax
$.ajax({
url: pageLocation,
//on success, set the html to the responsetext
success: function(data){
$("#biocontent").html(data);
},
dataType : "html"
});
根据您的更新,您是否尝试过$(“#biocontent”).load(页面位置)?这似乎与你正试图做的是一致的
$(function(){
//bind a click event to the nav links
$("#links a").bind("click", function(e){
//keep the links from going to another page by preventing their default behavior
e.preventDefault();
//this = link; grab the url
var pageLocation = this.href;
$("#biocontent").load(pageLocation);
});
});
我建议改用jQuery
load
函数:
$(document).ready(function(){
$("#links a").bind("click", function(e){
e.preventDefault();
var pageLocation = $(this).attr('href');
$("#biocontent").load(pageLocation,function(){
alert('Loaded!');
});
});
});
这是检索数据的最简单、最有效的方法,基于您的代码,我假设这只是HTML
此外,在最近的编辑中,定义pageLocation时缺少分号
希望这有帮助 所以我能够弄明白,这与使用chrome和访问我的c驱动器上的文件有关,当使用safari时,它起作用了,现在我只是有一个jquery冲突的问题,但我现在将对此进行一些研究,感谢所有的帮助人员。您的成功处理程序应该将“数据”作为HTML(除非这是调用并获取JSON或XML对象,否则处理程序的签名为success(data,textStatus,jqXHR),其中数据是返回的主体。
this.href
如果是锚定
且带有href
值,则应该可以工作:我做了更改'$(this.attr)(“href”);'而不仅仅是'this.href;'但是我对ajax调用有点困惑,如果你能帮我一点忙的话。另外,我注意到函数开头有一个错误,我有一个$,这就是你在页面加载时所指的吗?请参阅我的编辑。你的问题是同源策略。你只能向ot发出ajax请求她的页面与请求在同一域范围内。您的权利,但它似乎仍然不起作用,是否会因为脚本在我的文档中的位置而出现问题?错误“Access Control Allow Origin”是由您尝试访问其他域引起的。来自该其他域的响应应包含标题参数“Access Control Allow Origin”使用您的域名或*我会假设您的代码应该加载警报,如果它能够工作,并且因为它不工作,我不确定我的问题在哪里。您是否有机会使用单独的js文件将此代码放入其中?或者它与HTML放在同一页上?此代码是同一页,我喜欢标题中的Jquery“如果e link是“testvid.html”,您确定该文件与包含上述脚本的文件位于同一文件夹中吗?是的,它会打开它,只是不在div中