How to send a PUT/DELETE request in jQuery?

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

GET:$.get(..)

POST:$.post()..

What about PUT/DELETE?

13条回答
  •  自闭症患者
    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.

提交回复
热议问题