使用ajax请求时老是会有两个请求地址!

前端之家收集整理的这篇文章主要介绍了使用ajax请求时老是会有两个请求地址!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天用ajax进行局部刷新处理时,ajax请求地址的页面执行完毕之后又请求了网站首页的地址(通过httpwatch看出来的),代码如下:

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
$("#nextPage").click(function(){
$.ajax({
type: "post",
url: "<?PHP echo ROOTPATH;?>index.PHP/first/main/"+<?PHP echo $pageval;?>,
// data: "pageval="+pageval+"&uid="+uid,//用来传递参数pageval、uid
dataType: "json",
async:false,
cache:false,
success: function (msg) {
alert('返回值为:'+msg);
//$("input#showTime").val(data[0].demoData);
},
error: function (XMLHttpRequest,textStatus,errorThrown) {
alert("error");
//alert("err=="+errorThrown);
}
}); //end ajax
});
</script>

<a href="" id="nextPage" onclick='pages("<?PHP if ($pageval>0)echo ++$pageval;?>")'>下一页</a>

找了半天也没找到问题所在,后来灵光一现,猜测是href属性造成的,果然,去掉之后就正常了,只不过“下一页”这个按钮就没了链接效果


补充:后来在其他地方碰到同样的问题,但是想到了href="javascript:void(0)",于是乎好奇之下试了试,果然可以!

原文链接:https://www.f2er.com/ajax/164580.html

猜你在找的Ajax相关文章