63

It seems that using jQuery Ajax POST will pass parameters, but PUT will not. I looked at the current jQuery code and PUT and DELETE are not there. I looked at 1.4.2 jQuery and PUT and DELETE are there.

What is the workaround for passing parameters with a PUT request using the current version of jQuery?

4 Answers 4

95

Can you provide an example, because put should work fine as well?

Documentation -

The type of request to make ("POST" or "GET"); the default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Have the example in fiddle and the form parameters are passed fine (as it is put it will not be appended to url) -

$.ajax({
  url: '/echo/html/',
  type: 'PUT',
  data: "name=John&location=Boston",
  success: function(data) {
    alert('Load was performed.');
  }
});

Demo tested from jQuery 1.3.2 onwards on Chrome.

5
  • Have you tried getting parameters on server side? I use getParameter() in doPut() and params are not passed
    – andrei g
    Nov 7, 2011 at 15:45
  • i tried using HttpRequest and same result, so it's not jquery fault, but why latest Chrome doesn't pass PUT params?
    – andrei g
    Nov 7, 2011 at 19:38
  • if i append params to url it works, but it's just not right way to pass params
    – andrei g
    Nov 7, 2011 at 20:44
  • So Jayendra, thanks, are you saying servlet engine (i am using Tomcat) will not get params for PUT request and I should parse it myself from the input stream?
    – andrei g
    Nov 7, 2011 at 23:57
  • 3
    Demo link is broken
    – Edwin O.
    Mar 13, 2017 at 12:17
36

You can use the PUT method and pass data that will be included in the body of the request:

let data = {"key":"value"}

$.ajax({
    type: 'PUT',
    url: 'http://example.com/api',
    contentType: 'application/json',
    data: JSON.stringify(data), // access in body
}).done(function () {
    console.log('SUCCESS');
}).fail(function (msg) {
    console.log('FAIL');
}).always(function (msg) {
    console.log('ALWAYS');
});
3
  • The line with the comment // access in body - how can that data be accessed? In PHP when I try this and do some echoing both the $_GET and $_POST superglobals are empty arrays.
    – knot22
    Sep 8, 2019 at 23:50
  • 1
    It seems that adding $data = json_decode(file_get_contents('php://input'), true); to the PHP script allows the data to be accessed.
    – knot22
    Sep 9, 2019 at 0:05
  • 1
    NOTE: jQuery 1.9+ added method which is used instead of type so if you're coming here wondering why the docs aren't working, you might be running an older version of jQuery. Mar 31, 2020 at 20:14
11

For others who wind up here like I did, you can use AJAX to do a PUT with parameters, but they are sent as the body, not as query strings.

0
-9

Use:

$.ajax({
    url: 'feed/4', type: 'POST', data: "_METHOD=PUT&accessToken=63ce0fde", success: function(data) {
        console.log(data);
    }
});

Always remember to use _METHOD=PUT.

1
  • I would generally advise against parsing the data as a string as this prevents the app been dynamic and is harder to read than a simple object array. Additionally you can use type: 'PUT' instead of putting it in the data.
    – Jono20201
    Jul 7, 2015 at 10:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.