0

Hi I am trying to retrieve some data from webservice using AngularJS $http get.

I have the following code snippet:

In the servicesjs:

     .factory('BoothDesignatedCoordsService', ['$http', function ($http) {

var factory = {};

factory.getBoothDesignatedCoords = function (strBoothName, intFloorPlanID) {


 var sendToWS;
 var boothDesignatedCoords

    var JSONObj = {
        BoothName: strBoothName,
        FloorPlanID: intFloorPlanID
    };
    sendToWS = JSON.stringify(JSONObj)

    var urlBase = 'http://localhost:4951/wsIPS.asmx/fnGetBoothDesignatedCoords?objJSONRequest=' + sendToWS;
        return $http.get(urlBase) 
}
return factory;

}])

In the controllerjs:

 var boothDesignatedCoords = BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3).success(function (response, data, status) {
        console.log("successfully send booth name and floor plan id to ws");
        console.log("data " + data + " , status : " + status);
        console.log("data " + data);
        boothDesignatedCoords = data;

   for (var c = 0; c < boothDesignatedCoords.length; c += 2) {

   }

The $http get is successful as I am able to print "successfully send booth name and floor plan id to ws" in the browser console log. When I tried to print console.log("data " + data), it gives me some values of an integer array. That is exactly what I want. But in the controller I tried to assign data to the variable boothDesignatedCoords, the program does not run the for loop. Am I missing some code?

EDIT: I tried to trace the code ( trace the variable called "data" in the controllerjs) and it says "data is not defined"

1 Answers1

1

You appear to be confused about the methods available on the $http promise and their arguments. Try this

BoothDesignatedCoordsService.getBoothDesignatedCoords(strListShortListedBooth[i], 3)
.then(function(response) {
  var data = response.data
  var status = response.status

  console.log('data', data) // note, no string concatenation
  // and so on...
})

FYI, the success and error methods have been deprecated for some time and removed from v1.6.0 onwards. Don't use them.

I also highly recommend passing query parameters via the params config object

var urlBase = 'http://localhost:4951/wsIPS.asmx/fnGetBoothDesignatedCoords'
return $http.get(urlBase, {
  params: { objJSONRequest: sendToWS }
})

This will ensure the key and value are correctly encoded.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • I tried to do this : console.log(data ) and it says [object Object]. What should I do to get the actual data? – Hardyanto Putra Antoni Feb 08 '17 at 04:41
  • @Antoni if you do `console.log("data " + data)`, that will convert `data` to a string (hence the `[Object object]`). Instead, use `console.log('data', data)` – Phil Feb 08 '17 at 04:43
  • I mean i tried this console.log("boothDesignatedCoords.length " + boothDesignatedCoords.length); out the then function, it says boothDesignatedCoords.length is 0. The for loop is outside of the then function. – Hardyanto Putra Antoni Feb 08 '17 at 05:07
  • @Antoni anything outside the `then` function will run **before** anything inside it. See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Phil Feb 08 '17 at 05:27