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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2153917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:53:01  来源:igfitidea点击:

How to send a PUT/DELETE request in jQuery?

jqueryhttprequestput

提问by user198729

GET:$.get(..)

GET$.get(..)

POST:$.post()..

POST$.post()..

What about PUT/DELETE?

怎么样PUT/DELETE

回答by Darin Dimitrov

You could use the ajaxmethod:

您可以使用ajax方法:

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

回答by Jacob Relkin

$.ajaxwill work.

$.ajax将工作。

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

回答by Stepan Suvorov

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

我们可以扩展 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
    });
  };
});

and now you can use:

现在你可以使用:

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

copy from here

这里复制

回答by Pekka

Seems to be possible with JQuery's ajax functionby specifying

似乎可以通过指定使用JQuery 的 ajax 函数

type: "put"or type: "delete"

type: "put"或者 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?

大多数 Web 浏览器都提供 PUT、DELETE、HEAD 等方法吗?

回答by user2503775

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.

它基本上只是$.post()修改了方法参数的副本。

回答by moodboom

Here's an updated ajaxcall for when you are using JSON with jQuery > 1.9:

当您在 jQuery > 1.9 中使用 JSON 时,这是一个更新的ajax调用:

$.ajax({
    url: '/v1/object/3.json',
    method: 'DELETE',
    contentType: 'application/json',
    success: function(result) {
        // handle success
    },
    error: function(request,msg,error) {
        // handle failure
    }
});

回答by Pascal MARTIN

You should be able to use jQuery.ajax:

您应该能够使用jQuery.ajax

Load a remote page using an HTTP request.

使用 HTTP 请求加载远程页面。


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


您可以使用type选项指定应使用哪种方法:

The type of request to make ("POST" or "GET"), default is "GET".
Note: Other HTTP request methods, such as PUTand DELETE, can also be used here, but they are not supported by all browsers.

要发出的请求类型(" POST" 或 " GET"),默认为 " GET"。
注意:这里也可以使用其他 HTTP 请求方法,例如PUTDELETE,但并非所有浏览器都支持。

回答by antpaw

ajax()

ajax()

look for param type

寻找参数类型

Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

其他HTTP请求方法,如PUT和DELETE,也可以在这里使用,但并不是所有浏览器都支持。

回答by Paul Wand

For brevity:

为简洁起见:

$.delete = function(url, data, callback, type){

  if ( $.isFunction(data) ){
    type = type || callback,
    callback = data,
    data = {}
  }

  return $.ajax({
    url: url,
    type: 'DELETE',
    success: callback,
    data: data,
    contentType: type
  });
}

回答by Xanarus

You can do it with AJAX !

你可以用 AJAX 做到这一点!

For PUTmethod :

对于PUT方法:

$.ajax({
  url: 'path.php',
  type: 'PUT',
  success: function(data) {
    //play with data
  }
});

For DELETEmethod :

对于DELETE方法:

$.ajax({
  url: 'path.php',
  type: 'DELETE',
  success: function(data) {
    //play with data
  }
});