当前位置:   article > 正文

ajax 传值后台无法获取_ajax data传入list后c#后台接收不到参数

ajax data传入list后c#后台接收不到参数

今天 使用ajax+vue 做页面遇到很奇怪的问题,主要有如下的问题:

1、controller层 在使用 在使用@RequestParam接收参数时@RequestMapping中的method必须是RequestMethod.GET。

2、如果ajax中设置了 

contentType: "application/json; charset=utf-8",

dataType: "json",两个参数,那么在controller层中接收参数必须是包装类型(因为请求参数是可以不需要的,所以必须使用可空参数接收)如果controller中不改为包装类会报如下错误:

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.43 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Optional long parameter 'scenicspotid' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.</h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>Request processing failed; nested exception is java.lang.IllegalStateException: Optional long parameter 'scenicspotid' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Optional long parameter 'scenicspotid' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:582)
com.dianping.cat.servlet.CatFilter$CatHandler$3.handle(CatFilter.java:363)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$4.handle(CatFilter.java:463)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$2.handle(CatFilter.java:346)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$1.handle(CatFilter.java:219)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter.doFilter(CatFilter.java:84)
com.ctriposs.baiji.rpc.client.ribbon.HttpContextFilter.doFilter(HttpContextFilter.java:34)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
</pre><p><b>root cause</b></p><pre>java.lang.IllegalStateException: Optional long parameter 'scenicspotid' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.handleNullValue(AbstractNamedValueMethodArgumentResolver.java:191)
org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:97)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:582)
com.dianping.cat.servlet.CatFilter$CatHandler$3.handle(CatFilter.java:363)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$4.handle(CatFilter.java:463)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$2.handle(CatFilter.java:346)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter$CatHandler$1.handle(CatFilter.java:219)
com.dianping.cat.servlet.CatFilter$Context.handle(CatFilter.java:580)
com.dianping.cat.servlet.CatFilter.doFilter(CatFilter.java:84)
com.ctriposs.baiji.rpc.client.ribbon.HttpContextFilter.doFilter(HttpContextFilter.java:34)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
</pre><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/8.0.43 logs.</u></p><hr class="line"><h3>Apache Tomcat/8.0.43</h3></body></html>

,但是改为包装类后在controller层中接收到的值一直是null。

解决方案就是将ajax中的contentType和dataType两个参数删掉

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/814426
推荐阅读
相关标签
  

闽ICP备14008679号