如何使用cors和auth=user调用端点?
@http.route('/endpoint', type='json', cors='http://127.0.0.1:5173', auth='user', csrf=False, methods=['OPTIONS', 'POST'])
def endpoint(self):
return {'test'}
如果我从javascript调用这个端点
axios.post('http://127.0.0.1:8069/endpoint', {
jsonrpc: "2.0",
params: {},
}, {
headers: {"Content-Type": "application/json"},
withCredentials: true,
})
.then(content => {
console.log(content.data)
})
.catch(error => alert(JSON.stringify(error)))
我总是收到以下错误
Access to XMLHttpRequest at 'http://127.0.0.1:8069/endpoint' from origin 'http://127.0.0.1:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
我以前登录过
@http.route('/login', type='json', cors='http://127.0.0.1:5173', auth='none', csrf=False, methods=['OPTIONS', 'POST'])
def login(self, db, login, password):
http.request.session.authenticate(db, login, password)
return http.request.env['ir.http'].session_info()
和
axios.post('http://127.0.0.1:8069/login', {
jsonrpc: "2.0",
params: {
db: "db_name",
login: email,
password: password
},
}, {
headers: {"Content-Type": "application/json"},
withCredentials: true,
})
.then(content => {
console.log(content.data)
})
.catch(error => alert(JSON.stringify(error)))
而且这很管用。
发布于 2022-07-26 16:24:30
服务器应以Access-Control-Allow-Origin
报头为'*'
进行响应
https://stackoverflow.com/questions/73126621
复制相似问题