繁体   English   中英

无法通过请求从Ajax检索数据

[英]Cannot retrieve data from ajax with request

我在通过简单的应用程序从ajax帖子检索数据时遇到了问题,只是进行了一些测试。

我正在做一些简单的事情:

我有2节课:

Controller.java:

@RequestMapping(value = "/urlpost", method = {RequestMethod.GET, RequestMethod.POST} )
public urlPostTest(HttpServletRequest request, HttpServletResponse response) {
      request.setCharacterEncoding("UTF-8");

      String name  = request.getParameter("post_name");
      String age = request.getParameter("post_age");
      System.out.println("His name is: " + name);
      System.out.println("His age is: " + age);
}

PostingClass.js

function posting(){
  $.ajax({
    url: 'urlpost',
    method: 'POST',
    data: {
        'post_name': "Peter",
        'post_age': "22"
    },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function () {
        console.log("Send data: SUCCES.");
    }
});

}

Ajax可以正确访问URL,但是请求始终为null。

可能是什么问题呢?。

谢谢。

request.getParameter("post_name"); // works on application/x-www-form-urlencoded


要从application / json请求中获取数据,请使用如下代码:

String jsonStr = IOUtils.toString(request.getInputStream());
JSONObject jsonObj = new JSONObject(jsonStr);
String name = getString("name");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM