jQuery Ajax PUT with parameters

后端 未结 4 1317
后悔当初
后悔当初 2020-12-08 04:12

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

相关标签:
4条回答
  • 2020-12-08 04:34

    Use:

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

    Always remember to use _METHOD=PUT.

    0 讨论(0)
  • 2020-12-08 04:38

    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');
    });
    
    0 讨论(0)
  • 2020-12-08 04:42

    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.

    0 讨论(0)
  • 2020-12-08 04:42

    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 讨论(0)
提交回复
热议问题