【问题标题】:Servlet send 400 error with dataServlet 发送带有数据的 400 错误
【发布时间】:2017-12-15 09:19:11
【问题描述】:

我希望我的servlet 返回带有数据的错误代码,以便客户端可以向用户指出请求有什么问题。 如exception数据无效时:

catch (UnvaildInputException UIE) {
            UnvaildCodes unvaildCode = UIE.getUnvaildCode(); //code 1 for example
            System.out.println(unvaildCode.getKey() + "-" + unvaildCode.getValue()); //prints error key-val
            System.out.println(unvaildCode.getValue());
            response.getWriter().print(HttpServletResponse.SC_BAD_REQUEST +":"+unvaildCode.getValue()); //trying to write a string as data
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST); //400
        }

现在在客户端(angularjs)我正在尝试访问数据:

httpService.sendPost(vm.details, '/some/url').then(function (response){
        $log.log(response);
        $log.log(response.data);
});

错误:

TypeError: Cannot read property 'data' of undefined
    at formDirectiveJS.js:91
    at angular.min.js:126
    at m.$eval (angular.min.js:140)
    at m.$digest (angular.min.js:137)
    at m.$apply (angular.min.js:141)
    at g (angular.min.js:93)
    at t (angular.min.js:98)
    at XMLHttpRequest.u.onload (angular.min.js:99)

【问题讨论】:

    标签: java angularjs servlets


    【解决方案1】:

    您需要在错误处理程序中处理错误,而不是成功处理程序。

    来自https://stackoverflow.com/a/35458437/1842905

    $http.get(url)
    .then(function (response) {
        console.log('get',response)
    })
    .catch(function (data) {
        // Handle error here
    });
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2018-08-08
      • 1970-01-01
      • 2019-09-08
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多