简体   繁体   中英

Request Aborted on posting params using ajax

By using a ANCHOR tag , I am trying to redirect to http://google.com via HREF but at the same time I am posting some parameters to my another page via AJAX .Redirection is working fine but the parameters posting request is getting aborted.
Here is my code

<script type="text/javascript">
 $(function(){    
   $('#c').click(function(){           
     $.post("mypage.php?param1=abc1212",function(data){
     });
   });
 return false;  
});
</script>
</head>
<body>
<a id="c" href="http://google.com" class='test'> Click 2 Call</a>

Now page perfectly move to google but the POST request which is mypage.php?param1=abc1212 is getting aborted.I dont know why?
I can see status = aborted in firebug
I have searched alot but didn't get clue
Please Guide me about that why this problem is occurring and what is the solution?

<script type="text/javascript">
 $(function(){    
   $('#c').click(function(){           
     $.post("mypage.php?param1=abc1212",function(data){
     });
   });

    window.location.href = "http://google.com";
 return false;  
});
</script>
</head>
<body>
<a id="c" href="#" class='test'> Click 2 Call</a>

What you need is preventDefault .

 $(function(){    
   $('#c').click(function(event){
     event.preventDefault();           
     $.post("mypage.php?param1=abc1212",function(data){
         window.location.href = "http://google.com";
     });
   });
 return false;  
});

preventDefault stops the anchor from redirecting when clicked so that you can do your ajax. Once your ajax is done then redirect to google.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM