0

I'm using ASP.Net web api along with AngularJS to build a SPA. At this step I'm trying to retrieve a value from the response of a http post request, actually the saved id of last entry. I am getting the appropriate response but can't figure out the exact format of code to retrieve the value of the id from the response. I just need to know the exact format, I tried response.value.data.id but it didn't work for me. I also tried JSON.stringify(response) which doesn't provide the value object in the string.

enter image description here

Here's the function -

$scope.save = function () {
    var Patient = {
        name: $scope.PatientName,
        age: $scope.PatientAge,
        contact: $scope.PatientContact,
        address: $scope.PatientAddress
    };
    var response = patientService.post(Patient);
    console.log(response);
    //$location.path('/patient/' + $scope.PatientContact);
}

And here's the http post request -

this.post = function (Patient) {
    var request = $http({
        method: "post",
        dataType: "json",
        url: "/api/PatientsAPI",
        data: Patient
    });
    return request;
}

1 Answer 1

1

The $http function is returning a promise instead of the actual data.

So instead of var response = patientService.post(Patient);

You can use

var id;
patientService.post(Patient).then(function(response){
  console.log(response);
  id = response.value.data.id; // might not be correct, adjust according to response;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed, just needed to use response.data.id instead of response.value.data.id, thanks a lot for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.