-2

I'm getting a response from a server, which returns a response which has a message. But I can't figure out how to convert the message to json

I tried just converting it like this

.then((response) => {
   return response.message.json();
})

But that didn't work

This is my current code

fetch(`http://127.0.0.1:80/grab`, {
        method: "post",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },

        body: JSON.stringify({
          token: token,
          key: "grab_links",
          index: 10
        }),
      })
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          console.log(data)
          const linksHTML = data
        });

So how would I do this?

  • You convert the response to json (assuming the response body is json). If the message is in the body, you will be able to access it from within the json result. – jme11 Mar 05 '23 at 16:06
  • Not sure what `message` is, but it's typically done with just `response.json()`. – Wyck Mar 05 '23 at 16:14

2 Answers2

0

As stated in the documentation, you can set the whole response to json and then call the message part. ( more info on https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch )

fetch("http://example.com/movies.json")
  .then((response) => response.json())
  .then((data) => console.log(data));

Or in your case:

fetch("http://example.com/movies.json")
  .then((response) => response.json())
  .then((data) => {
     return data;
  });

Another way of converting JSON to JavaScript is JSON.parse (more info on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse):

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

console.log(obj.count);
// Expected output: 42

console.log(obj.result);
// Expected output: true
VelizarStavrev
  • 199
  • 1
  • 9
  • the message is still not in json – Aksel Glyholt Mar 05 '23 at 16:14
  • Can you console.log the data when you receive it and show us what you're getting? – VelizarStavrev Mar 05 '23 at 16:21
  • The example above is trying to convert a JSON server response to JavaScript appropriate data, that's what we're going for, right? – VelizarStavrev Mar 05 '23 at 16:22
  • Yeah that's what I need Response ` [Prototype]] : Object constructor : ƒ Object() hasOwnProperty : ƒ hasOwnProperty() isPrototypeOf : ƒ isPrototypeOf() propertyIsEnumerable : ƒ propertyIsEnumerable() toLocaleString : ƒ toLocaleString() toString : ƒ toString() valueOf : ƒ valueOf() __defineGetter__ : ƒ __defineGetter__() __defineSetter__ : ƒ __defineSetter__() __lookupGetter__ : ƒ __lookupGetter__() __lookupSetter__ : ƒ __lookupSetter__() __proto__ : (...) get __proto__ : ƒ __proto__() set __proto__ : ƒ __proto__() ` – Aksel Glyholt Mar 05 '23 at 16:24
  • Are you sure that the server is sending you the correct data back? The prototype object is something added in addition ( https://stackoverflow.com/questions/52678921/what-are-prototypes-and-why-are-they-added-to-my-json-object ). – VelizarStavrev Mar 05 '23 at 16:48
  • You can open your network tab in the browser and make that request. Then you can click on the request in the network tab, go to response and you should see your expected data there. If it's not there, my guess is that you're having a server issue. – VelizarStavrev Mar 05 '23 at 16:49
-1
  fetch(url)
  .then((response) => response.json())
  .then((values) => {
     return data;
  });
  • Hi Syed! The question here seems to be a struggle with something deeper than just the plain use of fetch. Your answer is a plain code example with no context. You could improve the quality of your answer(s) in the future by describing what you are attempting to show with your code example. – Michal Mar 07 '23 at 14:33