ajax 傳值後臺無法獲取
今天 使用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: O
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兩個引數刪掉