I want to make a frontend with vue with odoo as the backend. But everytime i make a request via axios to odoo i get a CORS Error.
Following are two Controller methods in odoo.
@http.route('/login', type='json', auth='none', cors='http://127.0.0.1:5173', 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()
@http.route('/logout', type='json', auth='user', cors='http://127.0.0.1:5173', csrf=False, methods=['OPTIONS', 'POST'])
def logout(self):
return {'test'}
http.request.session.logout()
The code for the request in JS is the following
async function login() {
axios.post('http://127.0.0.1:8069/login', {
jsonrpc: "2.0",
params: {
db: "db_name",
login: email.value,
password: password.value
},
}, {
headers: {"content-type": "application/json"},
withCredentials: true,
})
.then(content => {
console.log(content.data)
})
.catch(error => alert(error))
}
async function logout() {
axios.post('http://127.0.0.1:8069/logout', {
"jsonrpc": "2.0",
"params": {}
}, {
withCredentials: true,
headers: {"Content-Type": "application/json"},
})
.then(content => {
console.log(content.data)
})
.catch(error => alert(error))
}
When i take the cors out of the controller methods i get the error
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
When cors='*' and withCredentials=true then
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
Login works when withCredentials is false and cors='*' or cors the ip. When it's true i get the following error
The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
Logout never works. Everytime there is a cors error. For example:
Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
What am i missing? How do i call the odoo api the right way?