Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception while requesting data in JS

Im sending a JSON-String with my Quarkus Server. Im requesting the data with a js-fetch request(cors enabled -> (server-side,client-side)). And Im getting a error message like shown below at the picture.

The error-message(client-side)
enter image description here

Java-GET

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@GET
public String list(){
System.out.println(FruitFactory.listToJsonString(fruits));
return FruitFactory.listToJsonString(fruits);
}

Server output (valid JSON-Format):

[{"name":"Apple","description":"Red, Yellow,Green… kinda
delicious"},{"name":"Banana","description":"Yellow… kinda
delicious"}]

Client (FECTH)

let res = document.getElementById('res')

function getRequest() {
    fetch('http://localhost:8082/fruits', {
        mode: 'cors',
        headers: {
            'Access-Control-Allow-Origin': '*'
        }
    })
        .then((req) => {
            console.log(req)
            return JSON.parse(req)
        })
        .then((data) => {
            console.log(data);
            //  res.innerHTML = data
        })
        .catch((err) => {
            console.log(err);
        })
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./script.js" defer></script>
</head>
<body>
    <h1 id="res"></h1>

    <div id="get" onclick="getRequest()">GET</div>
</body>
</html>

Quarkus-Ressources

quarkus.http.port=8082

quarkus.http.cors=true

quarkus.http.cors.origins=*

quarkus.http.cors.methodes=GET, POST, PUT, DELETE

quarkus.http.cors.exposed-headers=Content-Disposition

quarkus.http.cors.access-control-max-age=24H

quarkus.http.cors.access-control-allow-credentials=true

>Solution :

The object asynchronously returned by fetch is a response object, not the response body itself. If its content type is JSON, you can use its .json method:

fetch(...).then((req) => {
  console.log(req)
  return req.json();
}).then((data) => {...})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading