0

当使用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 方法。

4

1 回答 1

0

您可以使用request.getInputStream()打印请求正文部分。我建议你contentType: "application/json; charset=utf-8", 可以application/x-www-form-urlencodedHttpServletRequest request可以@Requestbody ..

于 2017-03-15T03:11:31.353 回答