无法接收 json 到 servlet post 方法
unable to receive json to servlet post method
我试图将 json
数据发送到我的 serlvlet。但是当我尝试接收时,我得到了 null
。下面是我的代码。我哪里错了?没看懂。
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function test(){
var dataa={
"otp":"abc",
"email":"abcabc"
};
$.ajax({
url: "AbcTest",
type: 'POST',
dataType: 'json',
data:
{ dataa: JSON.stringify(dataa)
},
contentType:'application/json; charset=utf-8',
success: function (response) {
console.log(JSON.stringify(response));
},
error: function(xhr, resp, text) {
alert("error");
console.log(xhr, resp, text);
}
});
}
</script>
</head>
<body>
<button onclick="test();">Submit</button>
</body>
</html>
AbcTest Servlet
package org.abc.ussd;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AbcTest")
public class AbcTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public AbcTest() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("dataa"));
}
}
我在 servlet
中的 dataa
中得到空值。我如何从 jsp 页面接收 json 数据到此 servlet。
当我将内容类型更改为
时,我能够解决这个问题
contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
我在控制台中得到以下输出
我试图将 json
数据发送到我的 serlvlet。但是当我尝试接收时,我得到了 null
。下面是我的代码。我哪里错了?没看懂。
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function test(){
var dataa={
"otp":"abc",
"email":"abcabc"
};
$.ajax({
url: "AbcTest",
type: 'POST',
dataType: 'json',
data:
{ dataa: JSON.stringify(dataa)
},
contentType:'application/json; charset=utf-8',
success: function (response) {
console.log(JSON.stringify(response));
},
error: function(xhr, resp, text) {
alert("error");
console.log(xhr, resp, text);
}
});
}
</script>
</head>
<body>
<button onclick="test();">Submit</button>
</body>
</html>
AbcTest Servlet
package org.abc.ussd;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AbcTest")
public class AbcTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public AbcTest() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("dataa"));
}
}
我在 servlet
中的 dataa
中得到空值。我如何从 jsp 页面接收 json 数据到此 servlet。
当我将内容类型更改为
时,我能够解决这个问题contentType: "application/x-www-form-urlencoded; charset=UTF-8;",
我在控制台中得到以下输出