*
The moose likes Spring and the fly likes Empty Ajax response with Spring 3 and jQuery 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 » Frameworks » Spring
Bookmark "Empty Ajax response with Spring 3 and jQuery" Watch "Empty Ajax response with Spring 3 and jQuery" New topic
Author

Empty Ajax response with Spring 3 and jQuery

Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Hi guys, I've been stuck on this problem all day. I want to get a JSON value from my servlet using jquery. Here's the relevant code:

Client side (jquery-1.5.2)



Server side (Spring 3 servlet)



My server logs show that getRatesInfo() was called. Also, if I open the URL in the browser, the browser asks me if I want to download application/json data. If I do and open the file, I see this:

{"rateTypeCode":"USD","buying":"49","selling":"51"}

Then, if I save this to a file named response.json and change the client code to:



It works. What am I missing??? Thanks in advance for your replies.
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
BUMP; also, I've already tried this:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Hmm, can't see anything obvious by inspection. This can happen if the JSON is malformed, but it looks ok.

Check the response and headers in a tool like Firebug to make sure nothing's amiss.

If I get a chance later today, I'll give it a run.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What is the response's contenttype set to?

Should be set to application/json.


Whoops... if I read it a bit more closely I see it asks you to download it with the correct content type.

Eric
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Thanks for your replies.

Here are the headers viewed with firebug:

Response Headers:
Date Wed, 04 May 2011 02:55:02 GMT
Transfer-Encoding chunked
Content-Type application/json
X-Powered-By Servlet/2.4 JSP/2.0

Request Headers:
Host localhost:7001
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin null

Nothing is displayed in the "Response" tab. What could be causing this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

That means no response body in this instance.

As that's likely a Spring thing, I'll shuffle this along to the Sporing forum for further diagnosis (with a pertinent change of subject).
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

So, you aren't using Servlets, you are using Spring MVC, which has Controllers.

Have you tried to step through the serverside code to see if it is even getting to the first line of code?

Have you tried to get to it via the URL directly in the browser to see if it gets to the server side code?

This will tell us if you are using the correct URL to access the method.

If it is getting into the code, you are returning the data into the body. Do you have the jackson jar file in your classpath? Are you also setting the response status to successful with @ResponseStatus(HttpStatus.SOMETHINGINTHEENUM) on top of that method?

Are you familiar with all the annotations Spring uses in the Spring RESTful Web Service module?

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Hi Mark,

Yes, I'm sure the correct method is being called. The "log.info("getRatesInfo() called");" line is definitely being executed and I'm seeing it in the server logs.

Also, like I said if I hit the URL in a browser, the browser prompts me to download "application/json" data (it's in the latter half of my original post).

If it matters, I've tried hosting the webapp on Tomcat 6 and Weblogic 9.2 with the same results.

Thanks a lot for the help guys.

Edit: Yes, I do have the jackson-all-1.7.6.jar in my classpath. I'll try setting @ResponseStatus and report back.

Edit 2:

My controller method now looks like this:



But the result is the same. Sorry I should have mentioned that Firebug was already reporting http status as OK even before I added this annotation.

Here are the server logs:

15484 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO RatesInfoController - getRatesInfo() called
15594 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter - Written [simulator.rates.RatesInfo@62a404] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@1fe30f8]
15594 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'ratesSim': assuming HandlerAdapter completed request handling
15594 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: Http Request: /RatesSimulator/rates/
15594 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
15594 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'ratesSim-servlet': ServletRequestHandledEvent: url=[/RatesSimulator/rates/]; client=[127.0.0.1]; method=[GET]; servlet=[ratesSim]; session=[null]; user=[null]; time=[172ms]; status=[OK]
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
BUMP; anybody?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

In your Spring configuration, you also have <mvc:annotation-driven/>?

Mark
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Yes. Here, I'll post the whole thing.

At this point I gotta say this is really frustrating and I'm ready to give up on this. Sorry guys.



Just in case I'm missing something in web.xml, here it is:

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Wait, I just noticed this.
Also, like I said if I hit the URL in a browser, the browser prompts me to download "application/json" data (it's in the latter half of my original post).

If it matters, I've tried hosting the webapp on Tomcat 6 and Weblogic 9.2 with the same results.


So, if you go to the browser and type the URL, then you get the json data back? If this is so, then it isn't a Spring issue and I would have to move this back to the JavaScript forum as something in that request is different from just putting it in the URL.

Thanks

Mark
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
OK. Also, bump.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Mark Martinez wrote:OK. Also, bump.


OK? What do you mean. I am a little confused. Is the URL in the browser working?

Thanks

Mark
zaq collerose
Greenhorn

Joined: May 10, 2011
Posts: 2
I've been fighting this for 2 days now with no success. If you happen to figure out what the problem is, please post the solution.

It certainly looks like a jQuery issue as the browser is detecting json correctly.

Cheers,
Zaq
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Hi,

Sorry I meant the URL does work in the browser; it prompts me to download application/json data. I agree with Zaq; this might be an issue with jQuery. However, like I said, I've tried different versions with no success.
zaq collerose
Greenhorn

Joined: May 10, 2011
Posts: 2
Mark,

I *think* I finally figured this out. For my situation at least, I was simply using an html file to test rather than serving it in the same container as the app. That makes the browser treat it as a cross-domain request. Cross-domain means JSON is no good and thus, you don't see the response come back.

When I put the same file into the app (served by the same container), it worked fine and I could see the response JSON.

Hope that helps.

Zaq
Mark Martinez
Greenhorn

Joined: Aug 25, 2010
Posts: 10
Hi Zaq, I've been doing things the same way you have (just an html for testing) and so, with great haste and fingers crossed, I will try your solution.

Edit: It worked!

Thanks for all the help guys. I hope this thread helps somebody else in the future. Though I'm still baffled why passing data around with json on cross-domain requests doesn't work.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Sweet. Congrats guys. I had run out of ideas. ;)

Mark
Ben Morse
Greenhorn

Joined: Dec 01, 2012
Posts: 7

There is a thing called CORS which can solve this problem. I don't have a good code sample, but check this out: http://enable-cors.org/
 
Consider Paul's rocket mass heater.
 
subject: Empty Ajax response with Spring 3 and jQuery