AngularJS(和 Spring ResponseEntity)中的错误报告

error reporting in AngularJS (and Spring ResponseEntity)

我有一个 AngularJS 前端调用一个应用程序服务,它只是 return 一些虚拟数据。

应用程序在此调用中间歇性失败,我不知道为什么。我在 Spring REST 服务中有 'logging'(控制台),这表明这里没有问题。我对它做了一些改动,以尽可能多地检查它。它正在 returning 一个 org.springframework.http.ResponseEntity 对象

//Service controller
 @RequestMapping(value = "/person", method = RequestMethod.POST)
public ResponseEntity<Long> createPerson(@RequestBody Person person) {

    System.out.println("Service Layer - Creating Person");

    Long newPersonId = personService.createPerson(person);
    System.out.println("Created new Person: " + Long.toString(newPersonId));

    ResponseEntity<Long> x = new ResponseEntity<Long> (newQuestionId, HttpStatus.OK);

    System.out.println("return code: " + x.getStatusCode().toString());

    return x;

}

这每次都没有问题!成功或失败 return 之前的最后一个输出是成功和新的 Id。此外,创建正在保存到内存中 (Tomcat),当我刷新并返回到列表页面时,人物已创建。

不过 AngularJS 我有问题。 AngularJS 服务接收 return 代码的方式有些间歇性。我一遍又一遍地看着它并尝试了各种不同的东西,但我无法深入了解它。以下信息可能不够,所以如果您愿意提供帮助,请告诉我您可能还需要什么。

我从

开始
//Post Question
factory.createQuestion = function (question) {
    return $http.post(serviceBase + 'question/' , question).then(
      function (response) {
        alert ("X1");
        alert ("X3: " + response.status);
        newQuestionId = response.data;
        return response.data;
         })
    };

这段代码经常被忽略。我改用 .success 和 .error 以为这是方法,但@Duncan 告诉我我的方法不对(感谢您提供信息!)

我把它放在这里是因为它产生了一个有趣的结果

//JS Service
factory.createPerson = function (person) {
        return $http.post('serviceurl/person' , person).success
        (
            function (response) {
                newPersonId = response.data;
                return response.data;
        }).error(function(http, status) {
                alert ('error');
                alert ('status:' + status);
            });
    };

结果是状态始终为 -1。当我将 post 更改为 put

$http.post('serviceurl/person'...

我收到 405 错误(因为它不存在)。这让我想知道 AngularJS 服务和 Spring ResponseEntity 对象是否没有通信?

我想知道是否可能是这种情况(这就是我在标签中添加 Spring 的原因),但我认为我做的一切都正确吗?我想我的问题虽然很基本,但我希望如此 - 谁能告诉我如何才能获得有关错误的良好信息。如果我使用 .then(),我根本得不到有关它为何失败的信息?!

为完整起见,这是 AngularJS 控制器...

//JS Controller 
$scope.submitPersonData = function () {
    personService.createPerson($scope.person)
    .then(function (newPersonId) {
        $location.path('/people');  
    });
};    

$http.success承诺returnsdata, status, headers, config。以该顺序。我不知道你从哪里得到 http, status, fnc, httpObj。但是你不应该使用 .success.error;使用 .then 代替。所以是这样的:

$http.post('/path/to/api', person).then(function(response) {
    //http 200 resolved
    //get http status from response.status
    //get data from response.data
}, function(response) {
    //an error has occurred.  get details from data
    //get http status from response.status
    //get data from response.data
});

您真正应该做的是在您的 api 中,您应该在您认为服务失败的地方添加一个 try/catch。我不太了解 java...但这就是你在 C# 中的做法:

[HttpPost]
public JsonResult CreatePerson(Person person)
{
    PersonService service = new PersonService(person);

    Dictionary<string, object> returnMessage = new Dictionary<string, object>();

    try
    {
        long newPersonId = service.CreatePerson();
        returnMessage.Add("message", "success");
        returnMessage.AdD("newPersonId", newPersonId);
    }
    catch (Exception exc)
    {
        returnMessage.Add("message", "error");
        returnMessage.Add("details", exc.Message);
    }

    return Json(returnMessage);
}