javascript – 使用jQuery将JSON对象发布到Spring 3控制器

我试图在 Spring 3中向控件发送一个 JSON对象.

我收到以下错误

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',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',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',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',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)

我的控制器:

@RequestMapping(value = MappingConstants.RULE_ASSIGNMENT,method = RequestMethod.POST)
 public String saveRuleAssignment(@RequestBody RuleAssignmentCO ruleAssignment) {

  return MappingConstants.REDIRECT + MappingConstants.RULE_ASSIGNMENT;
 }

我的jQuery javascript:

ruleAssignment = {
   titleId: titleId,typeId: typeId,seriesId: seriesId
            }

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

解决方法

jQuery不会自动将Javascript对象编码成JSON,您需要使用JSON.stringify()手动进行操作:
jQuery.ajax({ 
    url: 'ruleAssignment',data: JSON.stringify(ruleAssignment),success: function(result) { 
         console.log('here'); 
    } 
});

请注意,某些浏览器不支持JSON.stringify()作为内置方法,在这种情况下,您需要json2.js才能进行可移植性.

相关文章

jQuery插件的种类 1、封装对象方法 这种插件是将对象方法封装起来,用于对通过选择器获取的jQuery对象进...
扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间。 入门 编写一个jQuery插件开始于给...
最近项目中需要实现3D图片层叠旋转木马切换的效果,于是用到了jquery.roundabout.js。 兼容性如图: ht...
一、什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作。其中,既有异...
AMD 模块 AMD(异步模块定义,Asynchronous Module Definition)格式总体的目标是为现在的开发者提供一...