Jak wysłać żądanie PUT/DELETE w jQuery?

GET:$.get(..)

POST:$.post()..

A co z PUT/DELETE?

Author: Damjan Pavlica, 2010-01-28

12 answers

Możesz użyć metody ajax :

$.ajax({
    url: '/script.cgi',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});
 814
Author: Darin Dimitrov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-28 10:58:15

$.ajax zadziała.

$.ajax({
   url: 'script.php',
   type: 'PUT',
   success: function(response) {
     //...
   }
});
 108
Author: Jacob Relkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-04 09:35:18

Możemy rozszerzyć jQuery o skróty do PUT i 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
    });
  };
});

A teraz możesz użyć:

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

Kopia z tutaj

 67
Author: Stepan Suvorov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-01 13:03:44

Wydaje się być możliwe z funkcja ajax JQuery poprzez podanie

type: "put" lub type: "delete"

I nie jest obsługiwana przez wszystkie przeglądarki, ale większość z nich.

Sprawdź to pytanie, aby uzyskać więcej informacji na temat kompatybilności:

Czy metody PUT, DELETE, HEAD itp są dostępne w większości przeglądarek internetowych?

 28
Author: Pekka 웃,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:26

Z TUTAJ możesz to zrobić:

/* 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');
    }
});

To w zasadzie tylko kopia $.post() z dostosowanym parametrem metody.

 9
Author: user2503775,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-28 09:18:31

Powinieneś być w stanie użyć jQuery.ajax :

Załaduj zdalną stronę za pomocą HTTP Prośba.


I możesz określić, która metoda powinna być używana, z type Opcja :

Rodzaj żądania ("POST " lub "GET"), domyślnie jest"GET".
Uwaga: Inne Metody żądania HTTP, takie jak PUT i DELETE, może być również używany tutaj, ale nie są one obsługiwane przez wszystkich przeglądarki.

 5
Author: Pascal MARTIN,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-28 10:59:05

Ajax()

Poszukaj param type

Inne metody żądań HTTP, takie jak PUT I DELETE, mogą być tutaj również użyte, ale nie są one obsługiwane przez wszystkie przeglądarki.

 4
Author: antpaw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-28 10:58:40

Oto zaktualizowane ajax wywołanie, gdy używasz JSON z jQuery > 1.9:

$.ajax({
    url: '/v1/object/3.json',
    method: 'DELETE',
    contentType: 'application/json',
    success: function(result) {
        // handle success
    },
    error: function(request,msg,error) {
        // handle failure
    }
});
 4
Author: moodboom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-05 17:40:16

Możesz to zrobić z Ajaxem !

Dla PUT metoda:

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

Dla DELETE metoda:

$.ajax({
  url: 'path.php',
  type: 'DELETE',
  success: function(data) {
    //play with data
  }
});
 3
Author: Xanarus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-17 04:27:35

Dla zwięzłości:

$.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
  });
}
 2
Author: Paul Wand,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-04 09:49:45

Napisałem wtyczkę jQuery, która zawiera rozwiązania omówione tutaj z obsługą cross-browser:

Https://github.com/adjohnson916/jquery-methodOverride

Zobacz!

 1
Author: AndersDJohnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-06 19:41:44

Możesz dodać do hasha danych klucz o nazwie: _method z wartością 'delete'.

Na przykład:

data = { id: 1, _method: 'delete' };
url = '/products'
request = $.post(url, data);
request.done(function(res){
  alert('Yupi Yei. Your product has been deleted')
});

Dotyczy to również

 0
Author: mumoc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-12 22:36:26