如何在jQuery中发送PUT / DELETE请求?

GET$.get(..)

POST$.post()..

PUT/DELETE呢?


你可以使用ajax方法:

$.ajax({
    url: '/script.cgi',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});

$.ajax将起作用。

$.ajax({
   url: 'script.php',
   type: 'PUT',
   success: function(response) {
     //...
   }
});

我们可以扩展jQuery来为PUT和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
    });
  };
});

现在你可以使用:

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

从这里复制

链接地址: http://www.djcxy.com/p/41005.html

上一篇: How to send a PUT/DELETE request in jQuery?

下一篇: Rails POST, PUT, GET