分享今天的這個知識點之前先回顧一下使用ajax的目的:實現(xiàn)異步請求,客戶端發(fā)送的請求攜帶數(shù)據(jù)到達(dá)服務(wù)器,服務(wù)器接收到請求后進(jìn)行處理,然后返回處理后的數(shù)據(jù)。客戶端接收到返回的數(shù)據(jù)后對數(shù)據(jù)進(jìn)行解析,并通過局部刷新顯示數(shù)據(jù)。那在SpringMVC中是如何響應(yīng)Ajax請求的呢?
我們往下看
<!-- 添加jackson的依賴-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
<script src="js/jquery-3.6.1.js"></script>
</head>
<body>
<a href="javascript:showUser()">發(fā)送ajax請求,獲取用戶信息列表</a><br>
<div id="userDiv">div,用來顯示服務(wù)器返回的數(shù)據(jù)</div>
<script type="text/javascript">
function showUser() {
//使用jQuery封裝的ajax()發(fā)送ajax請求
$.ajax({
url:"${pageContext.request.contextPath}/list.action",
type:"get",
dataType:"json",
success:function (userList){
var data=""
$.each(userList, function (i, user){
data +=user.name + " ---- " + user.age + "<br>"
})
//將拼接好的數(shù)據(jù)回顯在div中
$("#userDiv").html(data)
}
}
)
}
</script>
</body>
</html>
package com.example.controller;
import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class AjaxRequestAction {
//只有當(dāng)請求路徑和ajax請求類型都對上時,目標(biāo)方法才會被調(diào)用
@RequestMapping("/list")
@ResponseBody
public List<User> ajaxRequest(){//User類含有屬性:name(String), age(int),無參和全參構(gòu)造方法,全屬性的getter,setter,toString方法
List<User> users=new ArrayList<>();
User u1=new User("荷包蛋", 20);
User u2=new User("餃子", 21);
User u3=new User("橘子", 22);
users.add(u1);
users.add(u2);
users.add(u3);
return users;//SpringMVC框架會自動將對象數(shù)組轉(zhuǎn)化為json數(shù)據(jù)格式返回給前端ajax請求
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 添加包掃描-->
<context:component-scan base-package="com.example.controller"/>
<!-- 這里暫時不需要配置視圖解析器,因為這里響應(yīng)ajax請求,直接將請求到的數(shù)據(jù)以json格式返回給前端-->
<!-- 對專門響應(yīng)ajax請求的注解進(jìn)行注解的驅(qū)動注冊-->
<mvc:annotation-driven/>
</beans>
ps:本文為凱哥java實際工作中要到錯誤系列教程,在文章末尾會有本系列其他教程傳送門】
概要:
在使用spring mvc返回json格式的數(shù)據(jù)。我們都知道直接使用@ResponseBody注解就可以。
可是有時候,就算使用了這個注解還是會報406的錯誤。
正文:
在使用httpclient接收到數(shù)據(jù)返回json的時候提示406錯誤。
錯誤信息:
斷點查看:
HTTP/1.1 406 Not Acceptable [Server: Apache-Coyote/1.1,Content-Type: text/html;charset=utf-8, Content-Language: en, Content-Length: 1110, Date: Tue, 30 Aug 2016 14:04:13 GMT] org.apache.http.conn.BasicManagedEntity@2df3d87
解決辦法:
在spring mvc的配置文件中:
先看錯誤的配置:
在看正確的配置:
代碼:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 避免IE執(zhí)行AJAX時,返回JSON出現(xiàn)下載文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
添加了對json的支持。
再次運行:
斷點查看:
OK問題解決。
相關(guān)推薦:
《maven web項目啟動報錯 org.springfram》
《使用java做爬蟲獲取網(wǎng)絡(luò)資源下載403錯誤解決辦法 》
《解決eclipse在修改js或jsp卡頓現(xiàn)象》
《在用httpclient發(fā)送post報文請求錯誤解決》
《使用spring mvc 返回json報406錯誤解決》
ps:
如果您覺得本文對您有幫助,煩請您分享出去。謝謝!
歡迎
關(guān)注 【凱哥java】
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function(){
var params='{"id": 1,"name": "測試商品","price": 99.9,"detail": "測試商品描述","pic": "123456.jpg"}';
$.ajax({
url : "${pageContext.request.contextPath }/json.action",
data : params,
contentType : "application/json;charset=UTF-8",//發(fā)送數(shù)據(jù)的格式
type : "post",
dataType : "json",//回調(diào)
success : function(data){
alert(data.name);
}
});
});
</script>
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。