8

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
5
  • use this url: 'localhost/myapp?brown=fox&lazy=dog' Commented Jan 29, 2014 at 8:37
  • 1
    if you're using an IIS webserver and the jquery PUT or DELETE requests are returning 404 errors, you will need to enable these verbs in IIS Commented Jan 29, 2014 at 8:38
  • 3
    construct the url using $.param() Commented Jan 29, 2014 at 8:40
  • The http://localhost/myapp is not a website for human, but a REST like web service. I want to send a DELETE command because I want to delete something through the web service. Commented Jan 29, 2014 at 8:54
  • @ling.s I know how to send the DELETE request. I can't find information on that page on how to put the data into query string. Commented Jan 29, 2014 at 9:04

4 Answers 4

6

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"
    })
});
Sign up to request clarification or add additional context in comments.

Comments

3

Write your code like:

$.ajax({
    url: "http://localhost/myapp?brown=fox&lazy=dog",
    type: "DELETE",
    success: function(result){
        alert(result);
    }
});

Comments

0

I don’t think "DELETE" has supporting code to add data: { "brown": "fox", "lazy": "dog"} as "GET" does.

Do it yourself with Copy code

url: "http://localhost/myapp?brown=fox&lazy=dog",
type: "DELETE",

Note that DELETE is not supported by all browsers

Comments

-2

try

$.get(
    "http://localhost/myapp?brown=fox&lazy=dog&type=delete",
    function(data)
    {

    }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.