把在公司项目中遇到的不同http状态码,对应的解决问题,总结下(使用的都是Restangular):
1. 403 forbidden ,删除操作,发现是对应的接口写错,(很奇怪,没有报404,竟然是403),正确的写法是--$rootScope.BaseUrl+"/cmdb/delectTable/"+$scope.classFear.id,当时报错写法-$rootScope.BaseUrl+"/cmdb/delectable/"+$scope.classFear.id
2. 403 forbidden,删除操作,发现改变Restangular 的写法,正确写法是--Restangular.allUrl('/cmdb/deleteAttribute',$rootScope.BaseUrl+'/cmdb/deleteAttribute/'+$scope.classAttr.id).remove(),当时报错写法-Restangular.oneUrl('/cmdb/deleteAttribute',$rootScope.BaseUrl+'/cmdb/deleteAttribute/'+$scope.classAttr.id).remove()
3.400 Bad request,新增操作,请求无效,发现是向后台传递的数据格式不对,对象中包含多个对象,正确写法是--{"id":7,"attribute":"{'Description':'系统编号*','IsDisplayableInList':1,'Mode':0,'length':200,'OwnerName':'SystemId','Type':'STRING'}", "cmclass":{
"id":4,
"tablename":"test04"
}
},错误写法是 attribute对应的对象,外面还要多加一层“”,即使用JSON.stringify处理下,即可。
4. 415 unsupported media type,出现这种情况的原因是因为 请求头的 “content-type”值不对,改为“application/json”即可。