How to send a PUT/DELETE request in jQuery?

前端 未结 13 2241
别跟我提以往
别跟我提以往 2020-11-22 12:54

GET:$.get(..)

POST:$.post()..

What about PUT/DELETE?

相关标签:
13条回答
  • 2020-11-22 13:08

    You should be able to use jQuery.ajax :

    Load a remote page using an HTTP request.


    And you can specify which method should be used, with the type option :

    The type of request to make ("POST" or "GET"), 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.

    0 讨论(0)
  • 2020-11-22 13:10

    From here, you can do this:

    /* Extend jQuery with functions for PUT and DELETE requests. */
    
    function _ajax_request(url, data, callback, type, method) {
        if (jQuery.isFunction(data)) {
            callback = data;
            data = {};
        }
        return jQuery.ajax({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type
            });
    }
    
    jQuery.extend({
        put: function(url, data, callback, type) {
            return _ajax_request(url, data, callback, type, 'PUT');
        },
        delete_: function(url, data, callback, type) {
            return _ajax_request(url, data, callback, type, 'DELETE');
        }
    });
    

    It's basically just a copy of $.post() with the method parameter adapted.

    0 讨论(0)
  • 2020-11-22 13:13

    We can extend jQuery to make shortcuts for PUT and DELETE:

    jQuery.each( [ "put", "delete" ], function( i, method ) {
      jQuery[ method ] = function( url, data, callback, type ) {
        if ( jQuery.isFunction( data ) ) {
          type = type || callback;
          callback = data;
          data = undefined;
        }
    
        return jQuery.ajax({
          url: url,
          type: method,
          dataType: type,
          data: data,
          success: callback
        });
      };
    });
    

    and now you can use:

    $.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
       console.log(result);
    })
    

    copy from here

    0 讨论(0)
  • 2020-11-22 13:14

    Seems to be possible with JQuery's ajax function by specifying

    type: "put" or type: "delete"

    and is not not supported by all browsers, but most of them.

    Check out this question for more info on compatibility:

    Are the PUT, DELETE, HEAD, etc methods available in most web browsers?

    0 讨论(0)
  • 2020-11-22 13:14

    If you need to make a $.post work to a Laravel Route::delete or Route::put just add an argument "_method"="delete" or "_method"="put".

    $.post("your/uri/here", {"arg1":"value1",...,"_method":"delete"}, function(data){}); ...
    

    Must works for others Frameworks

    Note: Tested with Laravel 5.6 and jQuery 3

    0 讨论(0)
  • 2020-11-22 13:15

    $.ajax will work.

    $.ajax({
       url: 'script.php',
       type: 'PUT',
       success: function(response) {
         //...
       }
    });
    
    0 讨论(0)
提交回复
热议问题