Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

javascript - Using jQuery to post JSON object to Spring 3 controller

I'm trying to POST a JSON object to a controller in Spring 3.

I'm getting the following error:

2010-10-20 17:59:14,391 DEBUG [org.springframework.web.servlet.DispatcherServlet] Could not complete request
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]; nested exception is org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)
 at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:643)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:607)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
 at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2017)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
 ... 42 more
2010-10-20 17:59:14,391 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/cfs].[Spring MVC Dispatcher Servlet]] Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2017)
 at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1974)
 at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1331)
 at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:135)
 at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:643)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:607)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
 at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
 at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
 at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
 at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
 at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
 at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
 at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Thread.java:662)
2010-10-20 17:59:14,393 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/cfs].[default]] Servlet.service() for servlet default threw exception
org.codehaus.jackson.JsonParseException: Unrecognized token 'tilI': was expecting 'null', 'true' or 'false'
 at [Source: org.apache.catalina.connector.CoyoteInputStream@1509a99; line: 1, column: 9]
 at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1150)
 at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:324)
 at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidToken(Utf8StreamParser.java:1220)
 at org.codehaus.jackson.impl.Utf8StreamParser._matchToken(Utf8StreamParser.java:1188)
 at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:184)
 at org.codehaus.jackson.map.ObjectMapper._initForReading

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jQuery doesn't encode Javascript objects into JSON automatically, you need to do it manually with JSON.stringify():

jQuery.ajax({ 
    url: 'ruleAssignment', 
    type: 'POST', 
    dataType: 'json', 
    data: JSON.stringify(ruleAssignment), 
    contentType: 'application/json', 
    success: function(result) { 
         console.log('here'); 
    } 
}); 

Note that some browsers don't support JSON.stringify() as a built-in method, in this case you need json2.js for portability.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...