前台请求响应json

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="${pageContext.request.contextPath}/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function submits(){
var str = $("#str").val();
$.ajax({
type:'POST',
url:"${pageContext.request.contextPath}/springboot/getUserByPost",
data:str,
//dataType:'json',/告诉服务器,我要接收的是json格式数据
contentType:'application/json',//告诉服务器我发送的是json格式数据
success:function(data){
alert(syntaxHighlight(data));
}

});
}

function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, ' ');//第三个
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\u[a-zA-Z0-9]{4}|\[^u]|[^\"])*"(s*:)?|(true|false|null)|-?d+(?:.d*)?(?:[eE][+-]?d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class=" + cls + ">' + match + '</span>';;
});
}
</script>
</head>
<body>
<center>
<form id="testform">
内容:
<textarea rows="10" cols="50" id="str"></textarea>
<br /> <input type="button" value="提交" onclick="javascript:submits();"/>
</form>
</center>
</body>
</html>

原文地址:https://www.cnblogs.com/lichangyunnianxue/p/9708412.html