简体   繁体   English

AngularJS:在JSP中获取$ http.post数据

[英]AngularJS: Get $http.post data in JSP

I wrote the below code to send the $http.post request 我写了下面的代码来发送$ http.post请求

$http({
    url: '/OA_HTML/ReportColumnSelection.jsp',
    method: 'POST',
    data: JSON.stringify({myData : $scope.reportColumns}),
    headers: {'Content-Type': 'application/json;charset=utf-8'}
  })
  .success(function(response){
    $scope.viewColumns = response.columnData;
  })
  .error(function(response){
    //Error Log
    console.log('Inside saveReportColumns Error');
  });

Now the problem is i'm unable to get the "myData" JSON in ReportColumnSelection.jsp 现在的问题是我无法在ReportColumnSelection.jsp中获取“ myData” JSON

request.getParameter("myData");

Please let me know if i'm doing anything wrong. 如果我做错任何事,请让我知道。

AngularJS Version 1.4.7 AngularJS版本1.4.7

Code to save report columns: 保存报告列的代码:

$scope.saveReportColumns = function () { 
        console.log('Inside saveReportColumns'); 
        $http({ url: '/OA_HTML/eis/jsp/reporting/reports/newreport/columnselection/EISRSCReportColumn‌​SelectionAM.jsp', 
        method: 'POST', 
        data: JSON.stringify({myData:$scope.reportColumns}), 
        headers: {
            'Content-Type': 'application/json;charset=utf-8'
        } 
    }).success(function(response){}).error(function(response){ 
        //Error Log console.log('Inside saveReportColumns Error');
    }); 
};

The $http services default content type is application/json , which your JSP cannot parse. $http服务的默认内容类型是application/json ,您的JSP无法解析。

You can either, 你可以

  • change the content type of the request to application/x-www-form-urlencoded;charset=utf-8 (which might have other implications) 将请求的内容类型更改为application/x-www-form-urlencoded;charset=utf-8 (可能有其他含义)

or 要么

  • read it as a stream like below. 像下面的流一样阅读它。

InputStream body = request.getInputStream();

It might be a good idea to leave the content type as it is, and instead read the request body and parse it with a library such as google/json 最好保持内容类型不变,而是读取请求正文并使用诸如google / json之类的库进行解析

BufferedReader reader = request.getReader();
Gson gson = new Gson();

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

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