【问题标题】:Get json data from angularjs http post request thanks to servlet借助 servlet 从 angularjs http post 请求中获取 json 数据
【发布时间】:2015-11-20 06:56:05
【问题描述】:

感谢发布 http 请求,我尝试从 angularJS 客户端发送 JSON 格式的数据,并感谢 j2ee servlet。但我遇到了一个错误。由于我的 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;
}

Servlet

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 日志

//For Parameter names
param:tool
param:{ my correct data in json format}

也许我没有正确发送数据,但经过多次搜索后我不明白出了什么问题。

【问题讨论】:

  • 更改数据:数据,数据:jsonData,在您的 ajax 中

标签: json angularjs jakarta-ee servlets post


【解决方案1】:

请在您的代码中进行以下更改。

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);
     });
 }

【讨论】:

  • 首先,要解决CORS问题,我必须插入{headers: {'Content-Type': 'application/x-www-form-urlencoded'}。然后,我已经对此进行了测试,问题仍然存在。我真的不明白为什么数据以参数名称而不是值的形式存在..
  • 我可以看看数据包含什么吗?
  • 是的,它是:{"particularProperties":[{"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"}]}],"generalProperties":[{"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部分吗
【解决方案2】:

如果你想使用 json 我建议你实现 JAX-RS 服务而不是 Servlet,它会容​​易得多,尤其是如果你以后会使用一些更复杂的 json。

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

@Path("/myPath")
public class MyService {

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OutputData setParameters(InputData data, @QueryParam("tool") String tool) {

        System.out.println("Input data: " + data);
        System.out.println("Tool name: " + tool);
        ...
        return outputData;
    }
}

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

【讨论】:

  • 我没有选择。我必须用 servlet 做到这一点。不过谢谢
  • @Kael 在这种情况下检查这个post 它可能会为您提供一些提示/解决方案。
【解决方案3】:

我通过 post 找到了一个很好的答案。

$httpParamSerializer 方法修复了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2017-08-24
    • 2020-08-15
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多