Richiesta di patch usando AngularJS
-
20-12-2019 - |
Domanda
Sto lavorando su un'API usando Djang-Tastypie come back-end e angolarjs per front-end.Sto inviando una richiesta FRO Crud usando AngularJS $ http.Ottieni, pubblica, metti tutto bene ma quando sto cercando di inviare una richiesta di patch è presente che il metodo di errore è stato definito.Ho creato una fabbrica di chiamate API in una richiesta angolare ma patch non funziona lì.
angular.module('tastypieModule', ['ngResource']).
factory('apiCall', function($http, $resource) {
delete $http.defaults.headers.common['X-Requested-With'];
var apiCall = $resource('/api/v1/:type/:id/',
{type: '@type', username: '@userName', api_key: '@api_key', user: '@userID', id: '@id'},
{
get: {method: 'GET'},
post: {method: 'POST', headers: {'Content-Type': 'application/json'}},
del: {method: 'DELETE', headers: {'Content-Type': 'application/json'}},
update: {method: 'PUT', headers: {'Content-Type': 'application/json'}},
pupdate:{method:'PATCH',headers: {'Content-Type': 'application/json'}}
}
);
return apiCall;
});
function MyCtrl($scope,$resource){
$scope.edit=function(){
id=$scope.E_id
$http.pupdate('/api/v1/quizsetting/'+id+'/', editedquizsetting).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.editQuizSettingModal = false;
//$scope.quizsettinglist.objects[$scope.e_quizsettingindex]=data;
$(".message").append("object has been created successfully");
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}
.
Questo codice HTML
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<button type="button" ng-click="edit()">Edit</button>
</div></div>
.
Quando invio una richiesta di percorso utilizzando questo codice in console, mostra http.patch non è una funzione. Dimmi come posso configurare NG-app e servizi per inviare una richiesta di patch usando AngularJS.
Soluzione
Un problema comune con l'aggiunta di patch a AngularJS è che non ha un'intestazione predefinita del tipo di contenuto per quel metodo HTTP (che è applicazione / JSON; CHARSET= UTF-8 per Put, Post ed Elimina).E queste sono la mia configurazione del $ httpProvider per aggiungere supporto patch:
module.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.patch = {
'Content-Type': 'application/json;charset=utf-8'
}
}])
.