问题描述
我在调用由用户保护的 odoo Api (POST) 时遇到问题。下面是我与 POSTMAN 一起使用的 odoo api 代码。
@http.route('/route_api/routes',type="json",cors="*",csrf=False,auth="user",method='POST')
def routeById(self,id):
array_dict = []
data = None
try:
metro_routes = []
_logger.debug("current login user : " + str(id))
data = {'status':200,'rec_count':len(array_dict),'response':array_dict,'message' : 'Success' }
except Exception as e:
data = {'status':400,'rec_count':0,'message' : str(e) }
return data
下面是我的邮递员结果
但是当我试图从 IONIC 调用时,我在调试控制台上遇到了很多错误。下面是我的离子代码。
RoutesById(id: Number,_sessionId: string){
const configAll: any = {
headers: new HttpHeaders().set('Content-Type','application/json')
.set('Access-Control-Allow-Headers','X-Openerp')
.set('Cookie','session_id='+ _sessionId).set('X-Openerp',_sessionId) };
return new Promise((resolve,reject)=>{
this.httpCli.post(this.url + '/route_api/routes',{jsonrpc : '2.0',params : {id:id}},configAll).subscribe(data => {
resolve(data);
},err => {
reject(err);
});
});
}
我收到如下图所示的错误
这里是文字:
Refused to set unsafe header "Cookie"
Access to XMLHttpRequest at 'http://10.247.2.87:8069/route_api/routes' from origin 'http://localhost' has been blocked by CORS policy: Request header field x-openerp is not allowed by Access-Control-Allow-Headers in preflight response.
ERROR Error: Uncaught (in promise): $: {"headers":{"normalizednames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"UnkNown Error","url":"http://10.247.2.87:8069/route_api/routes","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://10.247.2.87:8069/route_api/routes: 0 UnkNown Error","error":{"isTrusted":true}}
POST http://10.247.2.87:8069/route_api/routes net::ERR_Failed
我确实尝试了登录 odoo api 并且没有这样的错误。不太了解 ionic 和 odoo,感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)