0

UPDATE: @MESSIAH told me to shorten this. Here is the bare bones version.

I cannot parse my response from an ajax call. I keep going in circles. Google is basically telling me that this is the problem. Well, no matter if I take out json in the ajax call or call parse. It will give me errors.

If parse directly without using JSON.parse(), I get: The first one is: Uncaught TypeError: Cannot read property 'time' of undefined Okay, so that means it is not parsed.

Currently, I am getting this because I am trying to parse: The one I am getting right now is: Uncaught SyntaxError: Unexpected token u Okay, so that means it is parsed.

Here is my ajax call:

var reply; function sendRouteRequest() { $.ajax({ type: 'GET', //contentType: "application/json; charset=utf-8", url: '/api/getReply', data: data, dataType: 'json', success: function(result) { reply = result; console.log(reply); console.log(reply.time); }, error: function(jqXHR, textStatus, errorThrown) { alert("Error: " + textStatus + " exception: " + errorThrown); } });//End of ajax } 

Here is the relevant code:

function parseJson() { //data = JSON.stringify(reply); jsonData = JSON.parse(reply); console.log(jsonData.time); } 

Here is what the reply looks like in console.

Object alert: Object points: Array[30] points: Array[30] time: 4.72 __proto__: Object status: Object __proto__: Object 

But console.log(reply.time) gives me undefined. Wut?

Going in circles.

Geeky

UPDATE: I am calling parseJson() in a function not shown here, $(document).ready(function(){ //blah blah blah}

Servlet returns application/json format

8
  • From your log, it looks already parsed (i.e., an object, not a string). On the other hand I don't see you calling parseJson anywhere... Commented Jul 17, 2013 at 19:03
  • if the content-type from the server is json, doesn't jQuery turn it into an object for you? Commented Jul 17, 2013 at 19:08
  • @bfavaretto I updated my answer to explain that. My problem is if it is already parsed, then why the first error, right? Commented Jul 17, 2013 at 19:13
  • 1
    @Geeky:believe me you wont get any replies unless you make your question 3 times shorter.just post the relevant part!!! Commented Jul 17, 2013 at 19:13
  • Maybe you're trying to read the response before it arrives, or it's out of scope. We'd have to see more from your code to tell. Commented Jul 17, 2013 at 19:24

1 Answer 1

1

This doesn't look good response. for eg why there are two points.

points: Array[30] points: Array[30] 

Can you print out the response what you get. It looks like javascript object and not JSON response.

if it is json response then it should look something like

[{"alerts" : {}, "points": [], "time": 4.72 ......}] 

I think you uncomment this line

dataType: 'json', 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, also to access what I need, I need to call the json object like console.log(reply.alert.time). Then it works!
I will go ahead and count this as the answer with both of our comments combined. This was really the start of the answer, but the key was accessing the structure as reply.alert.time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.