2

I have a hyperlink that is supposed to delete the record from the database as soon as that is clicked from jQuery Datatable row. My server-side code is a REST based Web Service which contains DELETE method to perform operations.

Here is My HTML:

<a href="#" data-toggle="modal" data-target="#" title="Delete"><i class="glyphicon glyphicon-trash"></i></a>

and here is how I am getting the Click event from the hyperlink:

 $('#StudentTable .glyphicon-trash').on('click', function () {
        alert("Hii");
        var table = $('#StudentTable').DataTable();
        var data = table.row(this.closest("tr")).data();
 });

I am not supposed to use an ajax call.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Lara
  • 2,821
  • 7
  • 39
  • 72
  • Why aren't you supposed to use AJAX call? – charliebrownie Oct 30 '15 at 09:58
  • @charliebrownie Ajax is not allowed here ..I donno why!! Requirement given by my Senior – Lara Oct 30 '15 at 09:59
  • 2
    if it's a REST Web Service why aren't you using `delete` method instead of `post` to follow the REST architecture? also wondering this too :) – charliebrownie Oct 30 '15 at 10:05
  • @charliebrownie I updated my post with DELETE method..Now please tell me how can get that ? – Lara Oct 30 '15 at 10:08
  • 2
    I was going to suggest using a form and forgetting JavaScript altogether, but then you changed your mind and decided not to do this with POST. It is impossible to make a DELETE request from a browser without using Ajax. – Quentin Oct 30 '15 at 10:08
  • @Quentin Is it not possible to invoke `DELETE` from Hyperlink – Lara Oct 30 '15 at 10:11
  • @Lara — That's what I just said. – Quentin Oct 30 '15 at 10:12
  • @Quentin Now i am thinking to use `MODAL FORM` which will contain DELETE button ..This will be the only way..Thanks for all your help.. – Lara Oct 30 '15 at 10:14
  • Err… that sounds like a library which uses Ajax behind the scenes, so it wouldn't fulfil your specified requirements. – Quentin Oct 30 '15 at 10:16
  • 2
    @Lara You restriction not to use ajax seems very odd as only POST and GET are supported within forms. (See: http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers ) You should clarify with your senior what they have in mind with that statement. – k0pernikus Oct 30 '15 at 10:45

1 Answers1

4

Without using XmlHttpRequest, you can perform POST requests by sending a form. You need to create a form with the field & values you want to send, append it to the document, then call the form's submit method.

form = $("<form action='[api endpoint]' target='[iframe id]' method='POST' style='display:none'></form>")
form.append "<input type='input' name='[paramName]' value='[paramValue]'>"
$("body").append(form)
form.submit()

You should set the form's target to an iframe, as sending the post will redirect to the server response. If the API is not on the same domain as the script, you will have to use CORS to access the response.

Also, as mentioned by others, requesting data deletion should be done using the DELETE http verb to comply with the REST philosophy.

Antoine
  • 5,055
  • 11
  • 54
  • 82
  • 1
    "Without using XmlHttpRequest, you can only perform GET requests" — You can make POST requests if you use a form. – Quentin Oct 30 '15 at 10:12