I have an AngularJS front end calling an app-service which just returns some dummy data.
The application intermittently fails on this call and I can't work out why. I have 'logging' (to console) in the Spring REST service which shows there is no problem here. I have altered it a little to check as much as I can here. It is returning an org.springframework.http.ResponseEntity object
//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;
}
This works without issue every time! Success or fail the last output before the return is of success and the new Id. Also the create is saving to memory (Tomcat) and when I refresh and go back to a list page the Person has been created.
I have issues in the AngularJS though. There is something intermittent in the way the AngularJS service is receiving the return code. I have looked at it over and over again and tried various different things but I can't get to the bottom of it. This is probably not enough info below so if you are willing to help just let me know what else you may need.
I began with
//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;
})
};
This code is often ignored. I changed to use .success and .error thinking this was the way but @Duncan tells me I have this the wrong way around (thanks for the info!)
I include this here though as it produced an interesting result
//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);
});
};
The result of this was that the status is always -1. When I changed the post to a put
$http.post('serviceurl/person'...
and I got a 405 error (as it doesn't exist). This makes me wonder if the AngularJS service and the Spring ResponseEntity objects aren't communicating?
I was wondering if this may be the case (this is the reason I have added Spring to the tags), I think I am doing everything properly though ? I guess as much as this my question though is very basic I hope - can anyone tell me how I can go about getting good information about the error. If I use the .then() I get no info about why it is failing at all ?!
For completeness this is the AngularJS controller...
//JS Controller
$scope.submitPersonData = function () {
personService.createPerson($scope.person)
.then(function (newPersonId) {
$location.path('/people');
});
};
$http
.success
promise returns data, status, headers, config
. In that order. I don't know where you're getting http, status, fnc, httpObj
from. But you shouldn't use .success
or .error
; use .then
instead. So it's like this:
$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
});
What you really should be doing is in your api, you should add a try/catch to where you think the service fails. I don't know java much... But this is how you'd do it in 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);
}