Can't I use Odoo endpoints from controller as API? from postman I can get this data. If I can then, my fetch function from JS layer providing this error. I'll be grateful if you can help me to find the problem.
Here is my controller structure,
@http.route('/task/get', type='json', auth='none', cors="*", csrf="False")
def get_task_data(self):
print("Function Called")
if request.httprequest.method == "GET":
data = {
'name': 'Tarikol Islam',
'age': '25',
}
# return json.dump(data) # Tried this one too
return data
//and Here is my JS fetch request,
async function getAllData(url = "http://localhost:8069/task/get") {
const response = await fetch(url, {
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrerPolicy: "no-referrer",
});
const data = await response.json();
console.log(data);
}
and the error(In tittle) show in chrome console is,
Uncaught (in promise) SyntaxError:
Bad Request
Invalid JSON data: ''
1.Ensure that the server is properly handling the request and returning a valid JSON response.
2. Test the API endpoint using tools like Postman or cURL to verify the response.
3.Check the URL to ensure it is correct and accessible.
4.If the server is returning a non-empty response but it still fails to parse as JSON,
double check the formatting of the response.Ensure that it starts with a {character and ends with a } character, representing a valid JSON object. If the response
is an array, make sure it starts with a [ character and ends with a ] character.
POSTMAN can got response successfully but have to give empty {} in body of 'GET' request.