This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Empty Ajax response with Spring 3 and jQuery

 
Mark Martinez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUMP; also, I've already tried this:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Martinez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUMP; anybody?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Spring configuration, you also have <mvc:annotation-driven/>?

Mark
 
Mark Martinez
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Also, bump.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet. Congrats guys. I had run out of ideas. ;)

Mark
 
Ben Morse
Greenhorn
Posts: 7
Chrome MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic