当我想删除用户时,为什么Restangular会出现406错误? 卷曲请求工作:
curl -X DELETE -v "http://localhost:3000/api/v1/users/1"
这是Restangular方法:
removeOne: function(user) {
var deferred;
deferred = $q.defer();
if (_.isUndefined(user.id) || _.isNaN(user.id)) {
AlertsServ.logError(err);
deferred.reject(err);
} else {
user.remove().then(function(result) {
return deferred.resolve(result);
}, function(err) {
AlertsServ.logError(err);
console.log(err);
return deferred.reject(err);
});
}
return deferred.promise;
}
和Rails方法(我使用Grape框架):
delete ':id' do
user = User.find(params[:id])
user.update(hidden: true)
user
end
请求服务器:
Referer: http://localhost:4400/users
Origin: http://localhost:4400
Host: localhost:3000
Content-Type: text/plain; charset=UTF-8
Content-Length: 263
Connection: keep-alive
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Accept: application/json, text/plain, */*
来自服务器的响应:
X-Runtime: 0.002188
X-Request-Id: 3aa91d50-c9f7-477d-812c-6a53b121a0d8
Vary: Origin
Server: WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)
Date: Wed, 15 Apr 2015 19:35:57 GMT
Content-Type: application/json
Content-Length: 69
Connection: Keep-Alive
Cache-Control: no-cache
Access-Control-Max-Age: 1728000
Access-Control-Allow-Origin: http://localhost:4400
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
Access-Control-Allow-Credentials: true
卷曲标题:
* Connected to localhost (127.0.0.1) port 3000 (#0)
> DELETE /api/v1/users/1 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 266
< Etag: W/"9a1f921e10b819161e3d40f70cb7e123"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 5ebd11b0-fbcc-4728-9aed-eda08b553ede
< X-Runtime: 0.028572
* Server WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25) is not blacklisted
< Server: WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)
< Date: Wed, 15 Apr 2015 19:39:59 GMT
< Connection: Keep-Alive
答案 0 :(得分:2)
只需将RestangularProvider.setDefaultHeaders({&#39; Content-Type&#39;:&#39; application / json&#39;})添加到module.config。