Javascript jQuery Ajax页面加载失败
我想当我点击一个属性为“linkdata”=“page”的链接时,将主体代码更改为加载图像,并在完成后将整个文档的HTML更改为结果。以下是我的当前代码:Javascript jQuery Ajax页面加载失败,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想当我点击一个属性为“linkdata”=“page”的链接时,将主体代码更改为加载图像,并在完成后将整个文档的HTML更改为结果。以下是我的当前代码: $('a[linkdata="page"]').each(function() { $(this).click(function () { var attribute = $(this).attr("href"); $("body").html('<center><img src="/ajax-loade
$('a[linkdata="page"]').each(function() {
$(this).click(function () {
var attribute = $(this).attr("href");
$("body").html('<center><img src="/ajax-loader.gif" /></center>');
$.ajax({ type: "GET", url: attribute }).done(function (data) {
$(document).html(data);
});
return false;
});
});
$('a[linkdata=“page”])。每个(函数(){
$(此)。单击(函数(){
var属性=$(this.attr(“href”);
$(“正文”).html(“”);
$.ajax({type:“GET”,url:attribute}).done(函数(数据){
$(文档).html(数据);
});
返回false;
});
});
结果是:
它会将主体的HTML代码更改为图像,并且请求总是失败(使用CodeIgniter,尝试使用.fail(function(){window.location=“/error/404”});
)$('a[linkdata=“page”]”)。单击(function(e){
e、 预防默认值();
$(“正文”).html(“”);
$.ajax({url:$(this.attr(“href”)})
.success(函数(数据){$(document.body).html(数据);})
.error(函数(x,s,e){alert('Warning!'+s+':'+e)});
});
$('a[linkdata=“page”]”)。在('click',函数上(k,v){
var属性=$(this.attr(“href”);
$(“正文”).html(“”);
$(文档).load({type:“GET”,url:attribute});
})
这样写('a[linkdata=“page”]”)。单击(函数(){。不要编写每个&然后单击函数。这个对象有更多的代码,这就是为什么我要使用它。每个都在它之前。不是拖拉,只是出于学习目的的好奇-通过ajax刷新整个文档而不是仅仅链接到页面或强制刷新有什么意义?看起来可能有一些限制阻止您使用rom访问或设置$(document).html()
:。此人也有问题:结果再次没有变化。你能解释一下为什么这是解决方案吗?请看。不知何故,你总是会得到一种带有空responseText的错误
$('a[linkdata="page"]').click(function(e){
e.preventDefault();
$("body").html('<center><img src="/ajax-loader.gif" /></center>');
$.ajax({url:$(this).attr("href") })
.success(function(data){$(document.body).html(data);})
.error(function(x,s,e){alert('Warning! '+s+': '+e)});
});
$('a[linkdata="page"]').on('click',function(k,v){
var attribute = $(this).attr("href");
$("body").html('<center><img src="/ajax-loader.gif" /></center>');
$(document).load({ type: "GET", url: attribute });
})