0

I have a button in my html:

<button id="confirm-download" onclick="submit(getStudentsDetails)" data-dismiss="modal" class="btn btn-success">Confirm</button>

and these are the submit and getStudentsDetails functions

function submit(callback) {
    $('#download_resumes').attr('action', '/api/studentsdetails/');
    $('#download_resumes').submit();
    callback()
}

function getStudentsDetails() {
    $('#download_resumes').attr('action', '/api/studentsdetails/');
    $('#download_resumes').submit();
}

Now these functions are referring to this form:

<form  id = "download_resumes" action="api/" method = "POST">

The problem here is that, only the second api (/api/studentsdetails/) is getting called here. I want both of these apis to be called onClick of the button.


The 2 APIs that need to be called are '/api/resumes/', '/api/studentsdetails/'.

Sreekar Mouli
  • 1,313
  • 5
  • 25
  • 49

2 Answers2

0

Use Handler of submit and then call your second function like the following

function submit(callback) {
    $('#download_resumes').attr('action', '/api/studentsdetails/');
    $('#download_resumes').submit(function( event ) {
  alert( "Handler for .submit() called." );
  callback()
});
}

function getStudentsDetails() {
    $('#download_resumes').attr('action', '/api/studentsdetails/');
    $('#download_resumes').submit();
}
Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28
  • Instead of `callback()`, I have writted `getStudentsDetails()` and tried this. Seems like both the API's are not getting called. – Sreekar Mouli Oct 16 '18 at 07:54
0

you can use ajax request to submit form and on success you can call that callback method

function submit(callback) {
//assuming callback is method name you want to call 
    $.ajax({
        url:'/api/studentsdetails/',
        data:$('#download_resumes').serialize(),
        success:function(response){
            window[callback]();
        }
    });        
}

window[callback] (); will call your callbackmethod on success refer this link for more information.

Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26
  • I have tried this code. The `api/resumes` is being called and after that I am getting this error in console: `GET http://localhost/api/studentsdetails/?resumes=007 404 (Not Found)`. My API doesn't take queries. How can I handle that? – Sreekar Mouli Oct 16 '18 at 07:51
  • 404 means there is no such url, may b you are calling wrong url. check your url may b it is wrong or spelling mistakes or given path is wrong there are many possibilities – Dhaval Pankhaniya Oct 16 '18 at 09:03
  • My bad. It needs to be a **POST** request not **GET**. I have added `type: 'POST'` now. The API now is being called but I am not getting the response. Basically its response is a csv file. – Sreekar Mouli Oct 16 '18 at 11:30
  • so you are getting data that you want to convert it to csv file right ?, or server is sending csv file as a response ? – Dhaval Pankhaniya Oct 16 '18 at 11:52
  • Server should csv file as response! – Sreekar Mouli Oct 16 '18 at 14:45
  • are you using mvc ? then check [this link](https://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult) for downloading files – Dhaval Pankhaniya Oct 16 '18 at 14:53