aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes jQuery.getJSON callback not executing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jQuery.getJSON callback not executing" Watch "jQuery.getJSON callback not executing" New topic
Author

jQuery.getJSON callback not executing

Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
I added the following code to my page.



I am getting the expected result from the server, namely a 200 status code, the mime type is application/json and the content is as expected. However, "test" isn't printing in the console. What gives?

Thanks,
Rob
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Are you sure your JSON response is well formed?


GenRocket - Experts at Building Test Data
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
The response is returning "true", which was working before. I cribbed it from some sample code in the jQuery tools validation docs.

Namely


Here we have used a simple return value true to indicate that the data was valid on the server-side.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I don't think you understand my question. More than "true" needs to be returned in a properly formatted JSON response from the server. Otherwise, $.getJSON can't parse it which can result in the behavior you are seeing. If you can show us your complete (scrubbed if need be) response from the server, we can help you determine if it is formatted properly.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Also note that if all you are returning from the server is "true", there is no need for $.getJSON. A simple $.get would suffice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

Newer versions of jQuery use more stringent JSON validation. Questionable JSON that might have worked before is not guaranteed to work now.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
Ah, I upgraded from jQuery-1.4.4 to jQuery-1.5.1.

I also tried the following and am now getting an "Uncaught SyntaxError: Unexpected token :"

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have no idea what the Gson API is. And as far as your problem goes, I don't think we care. Please show us the JSON that gets sent to the browser, not the code generating the JSON. If you are using Firefox, you should really be using Firebug. Otherwise, just log it in your servlet and copy and paste the results here.
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
This is the response

{"foo":"bar"}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

That should be fine.

Why the .getBytes()? I've never done that.
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
The getBytes was from an example I found that another developer had used. So if there is nothing wrong with the JSON, why am I getting "Uncaught SyntaxError: Unexpected token :" ? All my googling reveals is some stuff peculiar to JSONP responses from facebook and google.

Thanks,
Rob
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

I've never run into any problems using Gson in combination with response.getWriter().write(result).

Hmmm, grasping at straws... Have you actually looked at the response with Firebug or HttpFox?
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
Yes. In Firebug the response is

{"foo":"bar"}

I get the same thing in Chrome. (although the SyntaxError is from Chrome and the error is invalid label in FF).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

Could you post a SCCE servlet and HTML file that demonstrates the issue? I'd like to run it locally.
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
here is the html code, but I have no idea what an SCCE servlet is

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

SCCE = simple self-contained example
Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
I guess you mean something like this

Rob Dennett
Ranch Hand

Joined: Dec 07, 2010
Posts: 81
I forgot this import

import com.google.gson.Gson;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

The script in your HTML has syntax errors.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

And what you probably didn't want to hear: after fixing your syntax error, your example worked perfectly for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jQuery.getJSON callback not executing