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

来源:互联网 发布:软件服务类合同范本 编辑:程序博客网 时间:2024/04/29 04:45

今天用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)",于是乎好奇之下试了试,果然可以!

0 0
原创粉丝点击