Skip to Content
Menu
This question has been flagged
766 Views

Here's the code I'm using. It's Javascript and I run it inside a PHP file. I keep getting a CORS error and I'm not sure why. I know its a browser error but I can't get it resolved. I use Laragon on windows (nginx) as my server for development. I'm not very good at JS but I have developed several websites... This is just a test to see if I can actually get data from Odoo in an external server. The actual use case is a time and project manager for the shop floor.

async function fetchTimesheetsByEmployee(employeeName) {
    try {
        const apiUrl = 'https://**********.odoo.com'; // Replace with your Odoo instance URL
        const apiKey = '****************************************'; // Replace with your API key
        const database = '**********'; // Replace with your database name
        const username = '**********@*******.com'; // Replace with your Odoo username

        const response = await fetch(`${apiUrl}/web/dataset/call_kw`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                model: 'account.analytic.line',
                method: 'search_read',
                args: [[['name', '=', employeeName]], ['name', 'date', 'hours']],
                kwargs: { limit: 10 },
                context: {
                    lang: 'en_US', // Set the desired language
                },
            }),
        });

        const data = await response.json();
        console.log('Timesheets for', employeeName, ':', data.result);
        return data.result;
    } catch (error) {
        console.error('Error fetching timesheets:', error);
        return [];
    }
}

// Usage example
console.log("Odoo API Test:");
const employeeName = 'James Bergen';
fetchTimesheetsByEmployee(employeeName);
Avatar
Discard