2

I get the responses from the server in the following format.

{  
   "Success":true,
   "StatusCode":"000",
   "Result":[  
      {  
         "Id":"1",
         "Text":"Cash"
      },
      {  
         "Id":"1",
         "Text":"Check"
      },
      {  
         "Id":"3",
         "Text":"Bank"
      }
   ],
   "Error":null
}

When the responses is success. i.e "Success":true, I wanted to return only the Result part of the responses, from the interceptor.

In interceptor i have the following implementation.

Vue.http.interceptors.push(function (request, next) {
  this.$Progress.start()
  next(function (response) {  
    if (!response.body.Success) {
      this.$toastr('error', response.body.Error.Message)
      return false;
    }
    else{
      this.$Progress.finish()
    }    
  })
})

Currently i am using the separate services to fetch the information from the response body, when ever the request is made to the server.

Currently what i am doing is.

  var promises = [];
  promises.push(ListService.getStateList());
  promises.push(ListService.getById("address/city/get/" + to.params.id));
  Promise.all(promises).then(response => {
    next(p => {
      p.StateCollection = p.getResultData(response[0].body, "array");
      p.vModel = p.getResultData(response[1].body, "singleobject");
    });
  });

getResultData

    getResultData(data, item) {
     if (data.Success) {
       return data.Result;
     } else {
       if (item == 'array'||item == 'list') {
        return [];
       } else {
         return {};
       }
     }
   },

Is it possible to return only the Result part of responses from the interceptor itself?

I don't want to call the getResultData method, as i am doing currently to fetch the Result from the responses.

I wanted to bind the responses to object directly.

like Examples: p.vModel= response. no need to fetch the information from the body.

Rasik
  • 1,961
  • 3
  • 35
  • 72

0 Answers0