• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrestling with json

 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using a complete, rather than a success, callback?
 
J. Kevin Robbins
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic