简体   繁体   中英

jQuery.post callback not executing

When I make a call with jQuery.post, the callback is not being executed, but only if the call is made in a js file loaded into the webpage. If I copy and paste the same call into the javascript console of the browser, the callback get's executed. I know that the function is being called, because if I replace the call to jQuery.post with a simple alert(), it shows up. I've made sure the post request is completing (data is inserted into db on server side). I've also made sure that it is returning with a 200 code.

Also, this function is being called on demand when I click a button, so the DOM should be fully loaded by then.

Why would this be executed properly from the console, but not from a js file?

The problem ended up being that I wasn't returning false from the onclick callback, and so the page was refreshing every time I submitted the form. The refresh happened so fast that I didn't notice. If I moved the submit button out of the form, or returned false from the onclick callback, the expected behaviour occurred.

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