Submit Form when AJAX Call is done
Hello,
i would like to do an API call when a form is submitted. The result should be stored as hidden input.
My Problem is, that the form is submitted just before the ajax call is ready.
Here are some simplified code snippets:
- $("#MyForm").submit(function(){
- $.when( api_call() ).then( form_submit($(this)) );
- console.log('Not Submitted');
- return false;
- });
- function api_call() {
- console.log('AJAX Call');
- return $.ajax({ success: function(){
- // Set hidden input field
- console.log('AJAX Completed');
- }
- });
- }
- function form_submit(form) {
- console.log('Submitted');
- form.submit();
- }
In my console log:
- AJAX Call
- Submitted
- Not Submitted
- AJAX Completed
So the direction is clear:
The ajax call is sent and the form is submitted before the ajax call is ready...
How can i solve this problem?
Regards