Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP DELETE using jQuery ajax with parameters in query string

Tags:

jquery

I want to send a HTTP DELETE like this:

$.ajax({
    url: "http://localhost/myapp",
    type: "DELETE",
    data: {
        "brown": "fox",
        "lazy": "dog"
    }
});

The problem is jquery puts the data into the body of the request. I want the data to be put in query string, like http://localhost/myapp?brown=fox&lazy=dog.

Is there an option in $.ajax to do that? Or do I have to manually construct the query string?

jQuery 1.10.2.

UPDATE:

Context for my question:

  • I know how to use jQuery's $.ajax
  • I know how to send HTTP DELETE request
  • jQuery's $.ajax CAN send HTTP DELETE
  • the server can receive my HTTP DELETE request just fine (put another way: I have successfully sent a HTTP DELETE request to the server, the only problem is that the server misinterpret my request because it turned out jquery put the parameters in the body of the request while the server expects the parameters in query string)
  • I am sending the request to a web service
  • I have to use HTTP DELETE because that's how the provider of the web service set up the service
  • the parameters must be in the query string because the provider will only read the parameters in the query string and will ignore the request body
like image 897
Endy Tjahjono Avatar asked Jan 29 '14 08:01

Endy Tjahjono


2 Answers

There is a discussion of this behaviour on jQuery's GitHub page: https://github.com/jquery/jquery/issues/3269

The solution is to append the query string to the URL manually. jQuery's $.param function is helpful for this:

$.ajax({
    type: "DELETE",
    url: "http://localhost/myapp?" + $.param({
        "brown": "fox",
        "lazy": "dog"
    })
});
like image 164
benrwb Avatar answered Oct 11 '22 13:10

benrwb


Write your code like:

$.ajax({
    url: "http://localhost/myapp?brown=fox&lazy=dog",
    type: "DELETE",
    success: function(result){
        alert(result);
    }
});
like image 21
Girish Kumar Sinha Avatar answered Oct 11 '22 13:10

Girish Kumar Sinha