当使用GET方法调用服务时,它工作顺利,即打印 request.getParameter("userValue")。
但是在使用Post方法时,它会为 request.getParameter("userValue")打印null 。
HTML 代码:(jsonObject 具有有效的 json)
var myData = "userValue=" + jsonObject ;
jQuery.ajax({
type: "POST",
url: "http://localhost:8080/Webservice_JS_26Oct/FieldsToFile/write",
data: myData,
contentType: "application/json; charset=utf-8",
dataType: "json",
Java 代码:
@RequestMapping(value = "/FieldsToFile")
public class FileWriter {
@RequestMapping(value = "/write", method = RequestMethod.POST, produces = "application/json")
public String getData(HttpServletRequest request) throws IOException, IllegalStateException, ServletException {
String jsonString = request.getParameter("userValue") ;
System.out.println("jsonString = " + jsonString);
String myData = request.getParameter("myData") ;
我是新手,请告知如何使其适用于 POST 方法。