admin管理员组文章数量:1277525
I was having many problems trying to find the reason of why my ajax function was not working on Safari, Chrome and sometimes Firefox, but worked very well on IE. I made a little change, and everything start working perfect (in every browser), but I still dont know why, and I want to find out the main reason of this.
I had an Ajax function respuestas()
which insert some data on a database. This function is called by some links like this: <a onclick="respuestas()" href="link.html">LINk </a>
. So when I click on the link, the function takes the proper information and inserts it on the database and then go to link.html
. Again, this only worked for IE.
I insert an alert(xml.responseText)
to see the response that i was having. Safari, fireforx and chrome returns an empty alert.
I was tired of changing pages everytime I wanted to test my function, so I add a button calling my function (without going to another webpage) and IT WORKED!. Now, I have something like this: <a onclick="respuestas()" href="#">LINK </a>
and put window.location.href="link.html"
inside my ajax function, so the change of pages occur after the ajax response is pleted, and it is working very well.
But I do not entirely understand why this works and the other way does not.
I was having many problems trying to find the reason of why my ajax function was not working on Safari, Chrome and sometimes Firefox, but worked very well on IE. I made a little change, and everything start working perfect (in every browser), but I still dont know why, and I want to find out the main reason of this.
I had an Ajax function respuestas()
which insert some data on a database. This function is called by some links like this: <a onclick="respuestas()" href="link.html">LINk </a>
. So when I click on the link, the function takes the proper information and inserts it on the database and then go to link.html
. Again, this only worked for IE.
I insert an alert(xml.responseText)
to see the response that i was having. Safari, fireforx and chrome returns an empty alert.
I was tired of changing pages everytime I wanted to test my function, so I add a button calling my function (without going to another webpage) and IT WORKED!. Now, I have something like this: <a onclick="respuestas()" href="#">LINK </a>
and put window.location.href="link.html"
inside my ajax function, so the change of pages occur after the ajax response is pleted, and it is working very well.
But I do not entirely understand why this works and the other way does not.
Share Improve this question asked Feb 22, 2012 at 16:52 mauguerramauguerra 3,8585 gold badges33 silver badges37 bronze badges3 Answers
Reset to default 8This because the link element has also it's default listener. So, to prevent any extra action on click, you should prevent default action. The simple way to do this is to return false on click.
<a onclick="respuestas(this); return false;" href="link.html">LINk</a>
And your respuestas
in easiest way should be like this:
function respuestas(link) {
/* do what you need here */
window.location.href = link.href;
}
This is pretty primitive, but I believe you'll get the idea.
What's happening here is that your browser was navigating to the next page before repusetas()
was able to execute. In order to ensure that your script is going to fire before the browser follows the link, you need to take control of the click event. In jQuery it works like this:
$('a').bind( 'click', function( event ){
event.preventDefault();
repuestas();
var href = $(this).attr('href');
window.location.href = href;
});
Try this Jquery code
function respuestas()
{
$.ajax({
type: "post",
url: "insert.php?data="+data,
success : function(data){
window.location.href="link.html";
},
error : function(){
alert("Could not ");
}
});
}
本文标签: javascriptAjax not working with Href (solution)Stack Overflow
版权声明:本文标题:javascript - Ajax not working with Href (solution) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741208035a2358542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论