How to send a PUT/DELETE request in jQuery?

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

GET:$.get(..)

POST:$.post()..

What about PUT/DELETE?

13条回答
  •  迷失自我
    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

提交回复
热议问题