2

I'm trying to call asmx service using AngularJS $http. But i'm unable to get data from web service. I'm getting a success callback, but the response is the entire html page instead of the object returned as response.

Here is my C# asp.net web service code:

public class DataService : System.Web.Services.WebService
{

    [WebMethod]
    public Hello HelloWorld()
    {
        Hello h = new Hello();
        h.Message = "Hello World";
        return h;
    }
}

public class Hello
{
    public string Message { get; set; }
}

And here is the AngularJS code

    function FormController($scope,$http) {

    $scope.submit = function (user) {
        var url = '../../Services/DataService.asmx?op=HelloWorld';
        $http({
            method: "get",
            url: url
        }).success(function (data, status, headers, config) {
            console.log(data);
        });
    }

    FormController.$inject = ['$scope','$http'];
}

the above method is called on form submit.

iJade
  • 23,144
  • 56
  • 154
  • 243

1 Answers1

0

Looks like the URL is incorrect.

Try:

function FormController($scope,$http) {

    $scope.submit = function (user) {
        var url = '../../Services/DataService.asmx/HelloWorld';
        $http({
            method: "get",
            url: url
        }).success(function (data, status, headers, config) {
            console.log(data);
        });
    }

    FormController.$inject = ['$scope','$http'];
}
ForkandBeard
  • 874
  • 10
  • 28
  • well now it throws the error in console `Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.` – iJade Jun 20 '14 at 09:53