ฉันกำลังพยายามอัปเดตข้อมูลผ่าน API ของฉันโดยใช้วิธี http Patch แต่ฉันได้รับการตอบสนองที่ไม่ถูกต้องหรือข้อผิดพลาดเซิร์ฟเวอร์ภายใน
นี่คือการโทร JSON ของฉัน:
$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
ฉันกำลังพยายามอัปเดตข้อมูลผ่าน API ของฉันโดยใช้วิธี http Patch แต่ฉันได้รับการตอบสนองที่ไม่ถูกต้องหรือข้อผิดพลาดเซิร์ฟเวอร์ภายใน
นี่คือการโทร JSON ของฉัน:
$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
คุณสามารถเพิ่มส่วนหัวที่จำเป็นได้โดยใช้พารามิเตอร์ตัวเลือกที่ 3 ของ $http.patch :
var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)
เอกสารประกอบให้ข้อมูลเกี่ยวกับตัวเลือกการกำหนดค่าที่กำหนดเอง
หากคุณต้องการเพิ่มส่วนหัวที่กำหนดเองให้กับทุกคำขอโดยอัตโนมัติ คุณสามารถใช้ https://docs.angularjs.org/api/ng/service/$http#interceptors :
angular.module('app').factory('HttpInterceptor', function () {
return {
request: function (config) {
if (config.method === 'PATCH')
config.headers['IF-Match'] = 'your-data';
return config;
}
};
});
angular.module('app').config(['$httpProvider', '$resourceProvider', function ($httpProvider, $resourceProvider) {
// Add the interceptor to the $httpProvider to intercept http calls
$httpProvider.interceptors.push('HttpInterceptor');
}])
แก้ไข: เพื่อตอบความคิดเห็นของคุณเกี่ยวกับวิธีรับข้อมูลจากคำขอ GET ใน http interceptor คุณสามารถสกัดกั้นการตอบสนองได้เช่นกัน:
angular.module('app').factory('HttpInterceptor', function () {
var etag = null;
return {
request: function (config) {
if (config.method === 'PATCH')
config.headers['IF-Match'] = etag;
return config;
},
response: function (response) {
if (response.config.method === 'GET')
etag = reponse.config.headers['e-tag'];
// Return the response or promise.
return response || $q.when(response);
},
};
});