为什么这不起作用? jquery ajax替换特定div中的内容
下面我的网站上有代码,但是当我点击这个特定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"
});
});
});
这是我在 html
但仍然不行,请告诉我你的想法。
编辑
所以我把这段代码的最小部分移到一个新文档中,现在它根本不起作用,但我觉得我在 Jquery 上遇到了问题,所以现在它是工作,我在
17XMLHttpRequest 无法加载文件:///Users/semaj4712/Desktop/WME%20Website/testvid.html。 Access-Control-Allow-Origin 不允许 Origin null。
我不知道这意味着什么,所以一旦我让它在这里工作,我就必须在实际站点上使用 Jquery 解决问题。
Below I have the code on my site, yet when I click on a link in this particular div
it just opens a new window, why is this??
$(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);
}
});
});
});
EDIT:
I still havnt figured this out but here is where I am, my script is as follows
<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"
});
});
});
and here is how I am using it in my html<div id="links">
<a href="testvid.html"><img src="img/thumbs/img3.jpg" /></a>
but still no go, please let me know what you think.
EDIT
So I took the minimal parts of this code and moved it into a new doc, and now it doesnt work at all, but i feel like I was having an issue with the Jquery, so now it is work and I am getting this error in the
17XMLHttpRequest cannot load file:///Users/semaj4712/Desktop/WME%20Website/testvid.html. Origin null is not allowed by Access-Control-Allow-Origin.
Which I have no idea what that means, so once I get it working here then I have to tackle the issue with the Jquery on the actual site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该使用您想要获取的指定类型来调用 ajax。
You should call ajax with specified type that you want to get.
我建议改用 jQuery
load
函数:这是检索数据最简单、最有效的方法,并且根据您的代码,我假设这只是 HTML。
此外,在您最新的编辑中,您在定义 pageLocation 时缺少分号。
希望这有帮助!
I suggest using the jQuery
load
function instead:It's the simplest and most efficient way to retrieve data and based on your code i'll assume thats only HTML.
Also, in your latest edit you are missing a semicolon when you define pageLocation.
Hope this helps!
根据您的更新,您尝试过 $("#biocontent").load(pageLocation) 吗?这似乎与您想要做的事情一致。
http://api.jquery.com/load/
Based on your updates have you tried $("#biocontent").load(pageLocation)? It seems to be in line with what you are trying to do.
http://api.jquery.com/load/
在成功回调中,您的数据对象需要是 JSON 对象才能包含
responseText
,因此您需要在中设置dataType
字段对'json'
的 ajax 调用。在您目前的情况下,您似乎正在检索 html 。查看 jquery.com 上的 ajax 文档以获取更多信息。尝试使用 chrome 中的
javascript 控制台
或 firefox 中的firebug
查看是否有错误。您可能会在页面加载中找到一个编辑:
您正在处理您的文件系统!由于浏览器强制执行同源策略,您无法跨文件系统进行 ajax 调用。这意味着您无法向其他服务器发出 ajax 请求。您需要将 html 页面放在同一域中才能完成请求。当您的域名是 www.mydomain.com 时,您所做的就像从 www.google.com 请求数据一样。您只能从 www.mydomain.com 请求数据。对你来说最好的办法是在你的机器上设置一个 apache 服务器。
In the success callback, your data object needs to be a JSON object in order to contain
responseText
so you would need to set thedataType
field in the ajax call to'json'
. In your current situation, it looks like you are retrieving html back. Check out the ajax documentation on jquery.com for more info.Try using the
javascript console
in chrome orfirebug
in firefox to see any errors. Your likely to find one on the page loadEdit:
You are working on your file system! You can't make ajax calls across your file system due to same origin policy enforced by browsers. This means you can't make ajax requests to other servers. You would need to put your html pages on the same domain in order to complete the request. WHat you are doing is like requesting data from www.google.com, when your domain is www.mydomain.com. You can only request data from www.mydomain.com. The best thing for you to do would be to set up an apache server on your machine.
所以我能够弄清楚,它与使用 chrome 和访问我的 c 驱动器上的文件有关,当使用 safari 时它有效,现在我只是遇到了冲突的 jquery 问题,但我现在会对此做一些研究,谢谢感谢所有的帮助者。
So I was able to figure it out, it had to do with using chrome and accessing files on my c drive, when using safari it worked, now I just have an issue with conflicting jquery but I will do some research on that now, thanks for all the help guys.