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:
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?
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]
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.
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
posted
0
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
posted
0
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
posted
1
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
posted
0
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.