Jak wysłać żądanie PUT/DELETE w jQuery?
GET
:$.get(..)
POST
:$.post()..
A co z PUT/DELETE
?
12 answers
Możesz użyć metody ajax :
$.ajax({
url: '/script.cgi',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
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) {
//...
}
});
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
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?
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.
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 jakPUT
iDELETE
, może być również używany tutaj, ale nie są one obsługiwane przez wszystkich przeglądarki.
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
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.
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
}
});
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
}
});
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
});
}
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!
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ż
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