On page load, I am attempting to make an ajax request using the AngularJS $http service to fetch JSON data from a web method located in my User.aspx.cs
page.
The web method is defined as follows:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public static List<Users> GetUsers()
{
DBUtil objUtils = new DBUtil(); //This class handles database connections
List<Users> list = new List<Users>();
string strQuery = "select * from TM_Users";
DataTable dt = objUtils.GetDataTable(strQuery);
for (int i = 0; i < dt.Rows.Count; i++)
{
list.Add(new Users {
FullName=dt.Rows[i]["FullName"].ToString(),
UserName=dt.Rows[i]["UserName"].ToString(),
Password=dt.Rows[i]["Password"].ToString(),
phNum=dt.Rows[i]["MobileNo"].ToString(),
EmailId=dt.Rows[i]["EmailAddress"].ToString(),
Usertype=dt.Rows[i]["UserType"].ToString(),
CenterId=dt.Rows[i]["HealthCenterID"].ToString()
});
}
return list;
}
The issue arises when trying to call the web method within a controller defined in a separate JavaScript file named Control.js
. The objective is to populate an ng grid with data retrieved from the database. Below is the code snippet:
var app = angular.module("myApp", ["ngGrid"]);
app.controller('myCtrl', function ($scope, $http, $location) {
$scope.location = $location;
var url1 = "User.aspx/GetUsers";
var myData;
$http.get(url1).success(function (data,status,headers) {
myData = data;
}).error(function (err) {
console.log(err);
})
$scope.gridOptions = {
data: 'myData'
};
});
Upon debugging, it was observed that the success handler executes successfully. However, the web method on the aspx.cs
page does not execute as expected. Instead, the response received in the success handler's data is the entire HTML content of the web page.
Any assistance or insights would be highly appreciated!