Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
10 ตอบกลับ
31381 มุมมอง

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.

คำตอบที่ดีที่สุด
Looking to enhance your brand’s reputation? <a href="https://reviewexpress.net/product/trustpilot-reviews/">Buy Trustpilot Reviews</a> can help you stand out! Get authentic feedback that drives trust and attracts customers. Contact us today at +918302803616 to learn more and start transforming your online presence!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Great rhythm and challenging gameplay! Even though it's simple,  Friday Night Funkin' is really addictive and makes me want to play more.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Retrieve data successfully using Postman but still error

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi Hru?

อวตาร
ละทิ้ง

seo service london http://truediamond.co.uk/Wow! Thank you! I permanently needed to write on my blog something like that.

คำตอบที่ดีที่สุด

How could i resolve this issue for my sell tickets online page

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

It's trying to parse the error response of a missing asset. 

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

thank you!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Thanks for the answer!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Any update?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด
  1. Make sure that your controller is correctly receiving the request and returning a valid JSON response. In your case, you are returning a dictionary object data. To ensure that it is returned as JSON, you can modify your code as follows:
pythonCopy codeimport json

@http.route('/task/get', type='json', auth='none', cors="*", csrf="False")
def get_task_data(self):
    if request.httprequest.method == "GET":
        data = {
            'name': 'Tarikol Islam',
            'age': '25',
        }
        return json.dumps(data)

By using json.dumps(data), you are converting the Python dictionary data into a JSON string.

  1. In your JavaScript code, you can modify the fetch request to handle the response properly:
javascriptCopy codeasync 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);
}

getAllData();

The response.json() method parses the response body as JSON, so you can access the data as a JavaScript object.

Make sure to replace http://localhost:8069/task/get with the correct URL and port of your Odoo instance.

With these modifications, your controller should return valid JSON data, and your JavaScript code should be able to fetch and handle the response correctly.

If you are still experiencing issues, please provide any additional error messages or details from the console that could help in identifying the problem.


อวตาร
ละทิ้ง
ผู้เขียน

I copied exactly what you write, but problem remains. Basically in returns it got a html response, and make an error.
undefined:1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
พ.ค. 22
14206
2
เม.ย. 22
4329
1
เม.ย. 22
3413
1
ก.ค. 25
3727
1
พ.ย. 23
10261