javascript - jquery $.post() getting canceled -
this question has answer here:
i'm getting "canceled" status whenever jquery $.post(). seems asynchronous problem? access local project http://127.0.0.1:8933/myproject/default/index.html
index.html:
<script> $(document).ready(function() { $('#mybutton').click(function() { var post_url = get_url(); // resolves "/myproject/default/process_func" var data = ...; do_action(post_url, data); }); }); </script> <a href="" id="mybutton"> click me! </a>
util.js:
function dopost(url, data) { $.post(url, data).then(dosuccess, dofail): function dosuccess(data) { alert('successful!'); } function dofail(data) { alert('failed!'); } } function do_action(url, data) { var jsondata = json.stringify(data); // other stuffs... dopost(url, jsondata); }
the idea post data server processing using json data. user click on anchor button, do_action()
fire, , it'll posted in dopost()
.
this seems work since i'm seeing json data passed processing function on server. however, i'd see alert message "failed!" pop every time though data has been processed. in chromium's debug panel, see under "network" tab post has status of "(canceled)" , entire line highlighted in red. type stated "pending".
i think asynchronous problem, i'm unsure how fix it.
use this, cancel default navigatin behavior of <a>
:
$('#mybutton').click(function(e) { // <-- add `e` here e.preventdefault(); // <-- add line var post_url = get_url(); var data = ...; do_action(post_url, data); });
i believe ajax request being aborted browser because when clicking link, although request sent off, link tries navigate new page browser must abort open ajax requests.
Comments
Post a Comment