• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Parsing JSON jQuery object issue

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I build the json object in a servlet, then I send it back to the client via ajax. Once back at the browser, I want to get the values. I did search high and low, but I could not find any code that did not throw an error.

Here is the example output transmitted back the client. I do set the application type in the response as 'application\json". I used Google's Simple JSON library.

in JavaScript, if I do a 'alert(jsonObj)' I get what appears to be the correct string:





 
Sheriff
Posts: 67590
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
You should not be setting the content-type header unless you are sending JSON, so remove that.

Also, if you want JSON, why did you set the datatype to "text"? Set it to "json". jQuery will then automatically parse the JSON for you.

(Hint: you really should pick up copy of jQuery in Action, 3rd Edition if you are going to be using jQuery. )
 
M Burke
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You should not be setting the content-type header unless you are sending JSON, so remove that.

Also, if you want JSON, why did you set the datatype to "text"? Set it to "json". jQuery will then automatically parse the JSON for you.

(Hint: you really should pick up copy of jQuery in Action, 3rd Edition if you are going to be using jQuery. )



I appreciate that, thanks.

Also, I find that the json string returned from the server is not formatted correctly. It has '\"' rather than '"' characters. Perhaps I can convert it somehow that makes it valid.

This is an example:
 
Bear Bibeault
Sheriff
Posts: 67590
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
The JSON being returned from the server must be valid. If it's not, the server is the place to fix it.

And again, if using jQuery you don't (and can't) parse the returning JSON, it is already converted to a JavaScript object when passed to the handler. I don't understand why you continue to try to parse it.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic