This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes wrestling with json Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "wrestling with json" Watch "wrestling with json" New topic
Author

wrestling with json

J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 633
    
    7

I'm making my first foray into json and I need some advice.

Here's the servlet ProcessRequest method:

Here's the jsp:

And here's the javascript:

In the callback function above, "results" comes back as [object Object] and the alert box just says "undefined". That callback you see is for testing. it will eventually be "results.viewName" like in the alert box.
And here is the json that I'm getting back:

I've tried changing the dataType from json to text; no difference. I can't figure what I'm doing wrong in trying to parse the Json object. As I understand it, "results.viewName" should resolve to "fpbbprod". Is there another step to parsing the Json that I'm missing?


"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Why are you using a complete, rather than a success, callback?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 633
    
    7

Bear Bibeault wrote:Why are you using a complete, rather than a success, callback?


Only because based on this...

# success callbacks are then invoked, in the order they are registered, if the request succeeds. They receive the returned data, a string containing the success code, and the jqXHR object.
# complete callbacks fire, in the order they are registered, when the request finishes, whether in failure or success. They receive the jqXHR object, as well as a string containing the success or error code.

...there seems to be little difference between the two, except that complete always runs, like a finally block, so it seemed the best choice while I'm debugging this.

It's time for me to wrap up my day....I'll be back in the morning. I don't want you to think I'm ignoring your next reply; I appreciate your help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Your logic is flawed. The complete callback is called regardless of success or failure. Yet your code always assumes success. You also seem to be assuming that the same parameters get passed to either callback. Oops. The very text you posted says otherwise.

So complete is far from the best choice. It is, in fact, a very poor choice and the cause of your troubles.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 633
    
    7

Okay, changed the javascript like so:

The console tells me the statusText is "parsererror", yet JSONLint tells me the json is valid, so why the parser error?

I'll dig through firebug some more and see if I can get a better handle on this. Maybe it's not the json object that is throwing the parser error but rather the data or parameters I'm sending.... hmmm.....time to back up a couple of steps and look at the bigger picture.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Are there any extra characters being returned? Capture the response with Fiddler or something similar and see what is being returned from the server.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 633
    
    7

I've never used Fiddler. Very interesting tool, thanks for the tip; that led me to the solution. It showed that I wasn't getting any Json back so I went back to the servlet. I was putting the json object in the request. Duh. Obviously I need to read a good book on Ajax.

Now the servlet looks like this:

And the javascript like so:

And now everything is working as expected. So kids, if you try this at home, do not set your json object as a request attitribute, write it to the response output stream.

Thanks for the help, Eric and Bear.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

What version of jQuery? I had some JSON parsing problems with 1.5 that seems to have been resolved with 1.6.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 633
    
    7

I tested with Jquery 1.4.4. 1.5.1, and 1.6.1. But it wasn't a jquery problem, rather a PEBKAC issue. See my previous post for the explanation. The parser error was somewhat misleading. It was trying to parse nothing, so a null error of some sort would have been more helpful. But it was a learning experience and I now have a new tool (Fiddler) in my toolbox, so onward and upward.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrestling with json
 
Similar Threads
feedback on using reflection api
iterate doubt
json object send to JSF backing bean to xhtml page
DIV Tag Loading Behavior is Erratic
change event is firing twice