Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Java/JSON/jQuery issue

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote an app that will pass data via JSON. It is cross domain so I am using jQuery JSONP. My page works great when hitting this JSON echo site: http://echo.jsontest.com/key/value/one/two But when I hit my simple Java app, I get a jQuery parse error.

This is my basic condensed Java code:


I have tried every conceivable variation but still I get a parse error. When I hit the servlet directly, I see this which looks okay to me.

({"hello":"there"});


What can I try or do to make this work? What is the echo test site doing that I'm not doing in Java?
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In "normal" JSON the opening ( and trailing ); are not part of JSON. But with JSONP, you essentially create a function call. Your response appears to be missing the callback name (which should precede the opening paren).

Advice: don't create JSON by hand. Use a JSON library such as Gson or Jackson.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, Thank you and I will make sure to format code. I tried prefacing the response with callback parameter. Are there any good examples that you know of?
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked. It must have cached my non-working page my previous attempt or I am having serious post Thanksgiving delusions. Thanks..
 
Bear Bibeault
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
 
Does this tiny ad smell okay to you?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic