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"

4
  • I tried to trace the code ( trace the variable called "data" in the controllerjs) and it says "data is not defined" Commented Feb 8, 2017 at 3:52
  • Try using console log this way: console.log("data ", data); that will give you the data not just [Object] Commented Feb 8, 2017 at 3:53
  • You will get result in response.data Commented Feb 8, 2017 at 3:54
  • For the .success() function your first variable is the data returned by the $http.get function, so the second variable would not be what you're looking for I think. Commented Feb 8, 2017 at 3:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried to do this : console.log(data ) and it says [object Object]. What should I do to get the actual data?
@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)
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.
@Antoni anything outside the then function will run before anything inside it. See stackoverflow.com/questions/14220321/…

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.