0

I want to get the data from the webservice using angular js. pls help.. .

    $http(
                {
                    url: "xxx.asmx/GetLogin",
                        method: "GET",
                    params: { username: "admin", password: "admin" }
                }

                ).success(function (res) { alert(res); });

I get the result like this:

<?xml version="1.0" encoding="utf-8"?><string xmlns="xxxx">[{"UserID":2,"UserName":"Admin","UserType":1,"Password":"admin","Company":1,"CreatedDate":"\/Date(1411151400000)\/"}]</string>

How can i get the UserName and UserID from this string?

eladcon
  • 5,815
  • 1
  • 16
  • 19
  • This is a similar question: http://stackoverflow.com/questions/15490658/how-to-handle-xml-services-in-angularjs – klode Apr 11 '15 at 12:47

1 Answers1

0

Do you have access you the server?

By seeing the asmx extension I assume you are using a .Net server.
You will want to return a json instead of an xml for easy processing on the client end.

Read here about returning a json from an asmx service.

You can technically reach the UserName and UserID by string manipulation (regex or split) - but that's not the correct way to do this.

Here's an extra UGLY and BAD way to extract data from your string:

var str = '<?xml version="1.0" encoding="utf-8"?><string xmlns="xxxx">[{"UserID":2,"UserName":"Admin","UserType":1,"Password":"admin","Company":1,"CreatedDate":"\/Date(1411151400000)\/"}]</string>';

var obj = str.split('[')[1].split(']')[0];
eval('var myRes = ' + obj + ';'); // ugly as hell
console.log(myRes.UserID);
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99