简体   繁体   中英

Retrieving value from angularjs http post response in case of ASP.Net web API

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.

在此处输入图片说明

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;
}

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;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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