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?
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
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.
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.
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.
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 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.
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.