Getting json data

I am trying to get json data .In the javascript file when I do console.log(response) I see an output like Response{statuts: 200,…}.But when I go to the Network tab I can see my data.What am I doing wrong?

1 Like

Well, to know what you’re doing wrong it might first help to know what you’re doing. What is “the javascript file”. What elixir code do you have? What are you doing?

3 Likes

Basically I am trying to display some data without refreshing the whole page .
Javascript side…

someelement.addEventListener(“click”, e =>{

fetch(’/someroute’).then(function(response){

console.log(response)
})
})

Server side…
Controller is rendering the json object

1 Like

Can you show us some code? We can’t help you diagnose code we can’t see.

1 Like

Also, what outputs Response{statuts: 200,........}. ? That doesn’t look like javascript code. If you see the data in the network tab doesn’t that make this more of a javascript question?

1 Like

@benwilson512 this is the response I am getting

Response { type: “basic”, url: “http://localhost:4000/moreposts”, redirected: false, status: 200, ok: true, statusText: “OK”, headers: Headers, bodyUsed: false }

That is the response I am getting from this piece of javascript code

someelement.addEventListener(“click”, e =>{

fetch(’/someroute’).then(function(response){

console.log(response)
})
})

The json data is coming from the controller which can be seen in the Network tab.

1 Like

I’m going to give you some advice on asking questions so that you can help me to help you. The way this has gone so far has been singularly unhelpful.

A good question takes the form of:

  1. What you expect to happen. This would be the expected JSON response, and Elixir code used to send the expected response
  2. What has actually happened, highlighting how it’s different from what you expected.
  3. The Elixir code involved.

Finally, a good question makes use of any formatting tools available in the medium by which the question is being asked. This is stuff like code highlighting, because it makes it easier to read for the person who is trying to help you.

Other than the part where you want JSON data, I have no idea what you’re trying to do. I’ve asked twice now to see Elixir code, but you keep replying with the same javascript code.

I am not going to reply again without the following information:

  1. The elixir controller code
  2. The elixir router
  3. Is ‘/someroute’ the actual path?
  4. When you say “in the network tab” which request / response do you see it under? There will be several.

I promise I am not saying all of this just to be rude. I am trying to get you to provide us the information we need to help you.

6 Likes

there might be nothing wrong here. i think that is just what the fetch api returns. you can then get the actual data by calling methods on the response object. see https://developer.mozilla.org/en-US/docs/Web/API/Response.

you could for example have something like response.text().then(function (text) { console.log(text)})

2 Likes

@benwilson512 you are absolutely right about all the above points you have made… I am taking your advice seriously.My problem is that I drive taxis for a living;so whenever I get time I try and ask questions to clear my doubts.All of this is new to me but I promise to try and and ask questions in a better way…thanks

2 Likes

@manukall thanks a lot …that worked like a charm

1 Like

Further to what Ben said, remember that people are not mind readers :lol: you need to provide enough information so that others can visualise the situation and/or reproduce it.

You should also take a few minutes to familiarise yourself with Markdown :slight_smile: (it would be unfair to expect others to take the time out to help you when you haven’t taken the time to format your post and write up all the details adequately yourself).

2 Likes

sorry @AstonJ…I am taking every piece of advice you guys are giving…thanks

7 Likes