AngularJs 补丁请求
AngularJs Patch Request
我正在尝试使用 http 补丁方法通过 API 更新数据。但是我收到错误响应或内部服务器错误。
这是我的 JSON 电话:
$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
您可以使用 $http.patch 的可选第三个参数添加所需的 headers :
var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)
documentation 提供有关自定义配置选项的信息。
如果您希望自动将自定义 headers 添加到每个请求,您可以使用 $http interceptor :
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拦截器中,你也可以拦截响应:
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);
},
};
});
我正在尝试使用 http 补丁方法通过 API 更新数据。但是我收到错误响应或内部服务器错误。
这是我的 JSON 电话:
$http.patch(baseUrl + '/users/' + currentUserEmail,data).success(success).error(error)
您可以使用 $http.patch 的可选第三个参数添加所需的 headers :
var config = {headers: {'IF-Match': 'your-data'}};
$http.patch(baseUrl + '/users/' + currentUserEmail,data, config).success(success).error(error)
documentation 提供有关自定义配置选项的信息。
如果您希望自动将自定义 headers 添加到每个请求,您可以使用 $http interceptor :
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拦截器中,你也可以拦截响应:
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);
},
};
});