借助servlet从angularjs http post请求获取json数据

借助servlet从angularjs http post请求获取json数据,json,angularjs,jakarta-ee,servlets,post,Json,Angularjs,Jakarta Ee,Servlets,Post,由于post http请求,我尝试从angularJS客户机发送JSON格式的数据,并通过J2EEservlet获得数据。但是我遇到了一个错误。由于servlet中的getParameterNames方法,可以访问我的完整数据,而我无法以其他方式获取它 我不明白为什么我的数据是关键而不是价值 AngularJS客户端 setParametersForTools : function (toolName, data) { var jsonData = JSON.stringify(data

由于post http请求,我尝试从angularJS客户机发送JSON格式的数据,并通过J2EEservlet获得数据。但是我遇到了一个错误。由于servlet中的getParameterNames方法,可以访问我的完整数据,而我无法以其他方式获取它

我不明白为什么我的数据是关键而不是价值

AngularJS客户端

setParametersForTools : function (toolName, data) {
    var jsonData = JSON.stringify(data) // I try with json and stringify json
    var promise = 
    $http({
        url: configuration.root_url_api+"SetParametersServlet?tool="+toolName,
        method: "POST",
        dataType: 'json',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })          
    .then(function (response){
        console.log(response);
    }, function (error){
        console.log(error);
    })
    return promise;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String toolname = request.getParameter("tool"); //toolname is correct

    String json = request.getParameter("data");  // return null...

    Enumeration<String> paramsName = request.getParameterNames();
    for (;paramsName.hasMoreElements();) {
        String paramName=paramsName.nextElement();
        System.out.println("param:"+paramName);
    }
}
Servlet

setParametersForTools : function (toolName, data) {
    var jsonData = JSON.stringify(data) // I try with json and stringify json
    var promise = 
    $http({
        url: configuration.root_url_api+"SetParametersServlet?tool="+toolName,
        method: "POST",
        dataType: 'json',
        data: data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })          
    .then(function (response){
        console.log(response);
    }, function (error){
        console.log(error);
    })
    return promise;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String toolname = request.getParameter("tool"); //toolname is correct

    String json = request.getParameter("data");  // return null...

    Enumeration<String> paramsName = request.getParameterNames();
    for (;paramsName.hasMoreElements();) {
        String paramName=paramsName.nextElement();
        System.out.println("param:"+paramName);
    }
}

可能我发送的数据不正确,但经过多次搜索后,我不明白出了什么问题。

请在代码中进行以下更改

setParametersForTools : function (toolName, data) {
    $http.post(configuration.root_url_api+"SetParametersServlet?tool="+toolName, data)          
         .then(function (response){
               console.log(response);
          }, function (error){
               console.log(error);
     });
 }

如果您想使用json,我建议您实现JAX-RS服务,而不是Servlet,这将更加容易,特别是如果您稍后将使用更复杂的json

服务的伪代码(您还需要将jax rs配置添加到web.xml):


其中inputData和outputData是表示json的Java对象。

我找到了一个很好的答案


$httpParamSerializer方法修复了问题

在ajaxI'm not choice中更改数据:data,使用数据:jsonData。我必须用servlet来做这件事。但是谢谢you@Kael在这种情况下,检查此项可能会为您提供一些提示/解决方案。首先,要解决CORS问题,我必须插入
{headers:{'Content-Type':'application/x-www-form-urlencoded'}
。然后,我已经测试过了,问题依然存在。我真的不明白为什么数据是以参数名而不是值的形式出现的。我可以看看数据包含什么吗?是的,它是:
{“specialproperties”:[{“PROP_A”:[{“libelle”:“A”,“value”:50,“key”:“A”},{“libelle”:“b”,“value”:50,“key”:“b”},{“libelle”:“c”,“value”:50,“key”:“c”},{“libelle”:“d”,“value”:50,“key”:“d”}],“generalproperty”:[{“PROP_B”:[{“libelle”:“e”,“value”:65,“key”:“e”},{“libelle”:“f”,“value”:5,“key”:“f”},{“libelle”:“g”;“value”:10,“key”:“g”},{“libelle”:“h”,“value”:20,“key”:“h”}]}
我不更改语法,但更改数据是为了保密;)mh,对不起,我是java新手,无法打印。这是内存地址。^^^怎么做?@mukesh kamboj你能帮他做java部分吗