下午在寫SSMdemo的時候發現在ajax和控制器用json交換資料的時候出現了406,控制器可以接收到前端發來的json,控制器回傳卻出現406。
SpringMVC版本4.1.7
jackson-core,jackson-databind,jackson-annotations版本2.9.8
這是mvc.xml的部分配置
<mvc:annotation-driven/>
<!-- 2.靜態資源默認servlet配置
(1)加入對靜態資源的處理:js,gif,png
(2)允許使用"/"做整體映射
-->
<mvc:default-servlet-handler/>
<mvc:resources location="/resources/images/" mapping="/resources/images/**"/>
<mvc:resources location="/resources/css/" mapping="/resources/css/**"/>
<mvc:resources location="/resources/js/" mapping="/resources/js/**"/>
<!-- 3.配置jsp 顯示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="https://bbs.csdn.net/topics/org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="https://bbs.csdn.net/WEB-INF/jsp/" />
<property name="suffix" value="https://bbs.csdn.net/topics/.jsp" />
</bean>
這是控制器里要測驗的函式
/*測驗*/
@RequestMapping(value = "/detail/dto", method = RequestMethod.POST,
produces = {"application/json; charset=utf-8"})
@ResponseBody
private Result<AppointExecution> testDto(@RequestBody TestExecution execution) {
System.out.println(execution.getBooks());
AppointExecution execution1 = new AppointExecution(execution.getAppointment().getBookId(), AppointStateEnum.INNER_ERROR);
return new Result<AppointExecution>(true, execution1);
}
這里是jsp檔案里使用ajax的地方
<script type="text/javascript">
var dto={};
dto.appointment={
bookId:250,
studentId:100,
appointTime:null,
book:{bookId:5,name:"666",number:5}
};
dto.books=[{bookId:5,name:"666",number:5},{bookId:5,name:"678",number:5},{bookId:5,name:"678",number:5}];
$.ajax({
type:"POST",
url:"../detail/dto",
contentType : "application/json;charset=utf-8",
data:JSON.stringify(dto), //呼叫ajax的時候,data屬性必須這樣寫。必須
success:function(data,status){
alert(data.data.bookId);
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert(textStatus);
}
});</script>
瀏覽器f12查看
uj5u.com熱心網友回復:
已自行解決 ,更換了fastjson即可轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/116113.html
標籤:Web 開發
