GET:$.get(..)
POST:$.post()..
¿Qué hay de PUT/DELETE?
GET:$.get(..)
POST:$.post()..
¿Qué hay de PUT/DELETE?
Respuestas:
Podrías usar el método ajax :
$.ajax({
url: '/script.cgi',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
PUTo las DELETEsolicitudes devuelven errores 404, deberá habilitar estos verbos en IIS. He encontrado que este es un buen recurso: geekswithblogs.net/michelotti/archive/2011/05/28/…
"The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers."de: api.jquery.com/jQuery.ajax/#options
methodotype
$.ajax trabajará.
$.ajax({
url: 'script.php',
type: 'PUT',
success: function(response) {
//...
}
});
contentType: "application/json"
Podemos extender jQuery para hacer accesos directos para PUT y 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
});
};
});
y ahora puedes usar:
$.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
console.log(result);
})
copia desde aquí
Parece ser posible con la función ajax de JQuery especificando
type: "put" o
type: "delete"
y no es compatible con todos los navegadores, pero la mayoría de ellos.
Consulte esta pregunta para obtener más información sobre compatibilidad:
¿Están disponibles los métodos PUT, DELETE, HEAD, etc. en la mayoría de los navegadores web?
Desde aquí , puedes hacer esto:
/* 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');
}
});
Básicamente es solo una copia $.post()con el parámetro del método adaptado.
Deberías poder usar jQuery.ajax:
Cargue una página remota utilizando una solicitud HTTP.
Y puede especificar qué método debe usarse, con la typeopción :
El tipo de solicitud para realizar ("
POST" o "GET"), el valor predeterminado es "GET".
Nota: Otros métodos de solicitud HTTP, comoPUTyDELETE, también se pueden usar aquí, pero no son compatibles con todos los navegadores.
PUTo DELETE?
buscar tipo de param
Aquí también se pueden usar otros métodos de solicitud HTTP, como PUT y DELETE, pero no todos los navegadores los admiten.
¡Puedes hacerlo con AJAX!
Por PUTmétodo:
$.ajax({
url: 'path.php',
type: 'PUT',
success: function(data) {
//play with data
}
});
Por DELETEmétodo:
$.ajax({
url: 'path.php',
type: 'DELETE',
success: function(data) {
//play with data
}
});
He escrito un complemento jQuery que incorpora las soluciones discutidas aquí con soporte para navegador cruzado:
https://github.com/adjohnson916/jquery-methodOverride
¡Echale un vistazo!
Si necesita hacer un $.posttrabajo en un Laravel Route::deleteo Route::putsimplemente agregar un argumento "_method"="delete"o "_method"="put".
$.post("your/uri/here", {"arg1":"value1",...,"_method":"delete"}, function(data){}); ...
Debe funcionar para otros Frameworks
Nota: Probado con Laravel 5.6 y jQuery 3
Puede incluir en su hash de datos una clave llamada: _método con el valor 'eliminar'.
Por ejemplo:
data = { id: 1, _method: 'delete' };
url = '/products'
request = $.post(url, data);
request.done(function(res){
alert('Yupi Yei. Your product has been deleted')
});
Esto también se aplicará a
Aquí hay una línea simple que utilizo para poner más de una variable:
$.put("https://your-url.com",{item1:'new item1',item2:'new items2'});