2015-12-17 12 views
0

我努力讓自己在頁面加載一個Ajax請求itself.I我採用了棱角分明的js $ http服務,以擺脫它存在於我的User.aspx.cs頁面的Web方法的JSON數據。Web方法獲取執行

我的Web方法如下:

[WebMethod] 
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public static List<Users> GetUsers() 
    { 
     DBUtil objUtils = new DBUtil(); //This is my class for db connection 

     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; 
    } 

沒有與網絡沒有問題method.When我嘗試調用Web方法與$ HTTP的幫助是控制器伊娜單獨的JS文件中寫說Control.js。我的功能在這裏是,我正在使用ng網格,所以要將該網格與數據庫中的數據綁定。 如下面的代碼:

var app = angular.module("myApp", ["ngGrid"]); 

app.controller('myCtrl', function ($scope, $http, $location) { 
    //var myData = $http.get("User.aspx/GetUsers"); 
    $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' 
    }; 

}); 

當調試後來我發現是的研製成功處理越來越executed.But aspx.cs頁面上的Web方法沒有得到響應execute.The我在成功處理程序收到數據是在.aspx頁面

感謝你的幫助的整個HTML!

+0

中的WebMethod添加靜態像'公共靜態列表 GetUsers()' –

+0

還,如果得到請求,加上'[ScriptMethod(UseHttpGet = FALSE)]' –

+0

對不起他們did'nt解決我的問題。我得到一個錯誤,說明對象不支持屬性或方法「配置」在我的控制器 –

回答

1

改變你的HTTP GET調用諸如this.I點點添加的內容類型,一個空的數據對象和響應類型爲JSON。

var app = angular.module("myApp", ["ngGrid"]); 

app.controller('myCtrl', function ($scope, $http, $location) { 
    var request = 
    {   
    method: "GET", 
    url: "User.aspx/GetUsers", 
    data: {}, 
    headers: { "Content-Type": "application/json" }, 
    responseType: 'json' 
    } 

    $http(request).then(function (data,status,headers) { 
     myData = data; 
    }).error(function (err) { 
     console.log(err); 
    }) 
    $scope.gridOptions = { 
     data: 'myData' 
    }; 

}); 

}); 
+0

謝謝你這個作品。我會很高興你可以告訴我什麼是我做錯了 –

+0

順便說一句,當我在網絡方法 –

+0

中使UseHttpGet = true時,你實際上需要明確地告訴有關您所期望的數據和響應的內容類型的請求。 –

0

WEBMETHOD應該是一個static方法。

+0

感謝您與靜態..Still提醒..Edited同樣的錯誤出現 –

+0

數據在成功處理程序具有 –

+0

aspx頁面調試HTML和檢查是否的WebMethod被叫或不。 –