352229 (9) [Avatar] Offline
#1
Hi

I am using angular.js $http in asp.net application.

C# code
 [WebMethod]
    public static string GetCountry()
    {
        List<string> list = new List<string>();
        using (SqlConnection connection = new SqlConnection(@"Conn_String"))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand("select top 2 Title from Task", connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Convert.ToString(reader["Title"]));
                    }
                }
            }
        }
        // Serialize the results as JSON
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(list.GetType());
        MemoryStream memoryStream = new MemoryStream();
        serializer.WriteObject(memoryStream, list);

        // Return the results serialized as JSON
        string json = Encoding.Default.GetString(memoryStream.ToArray());
        return json;
    }


And in aspx page I am using following code

<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('app', []);
        app.controller('myController', function ($scope, $http) {

            var request = $http({
                method: "GET",
                url: "Default2.aspx/GetCountry",
                data:{}
            });

            request.success(function (response) {
            });

            request.error(function () {
                alert("Error in loading contents");
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div ng-app="app" ng-controller="myController">
            
    </div>
    </form>
</body>


But I am not able to call $http. Is there anything I am missing
Lukas Ruebbelke (107) [Avatar] Offline
#2
The structure of your $http call is wrong. It should follow this pattern.

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });


Let me know if you would like to me elaborate further.
352229 (9) [Avatar] Offline
#3
Hi Lukas

Thanks for the reply. Now I am getting success, but $http is not calling the server side code.