2013-09-29 129 views
2

我是AngularJS的新手。AngularJS:調用asp.net WebMethod

這是我的HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html ng-app="Resource"> 
<head runat="server"> 

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script> 

    <script src="JS/controller.js" type="text/javascript"></script> 

    <title></title> 
</head> 
<body> 
    <div ng-controller="ResourcesCtrl"> 
     <form id="form1" runat="server"> 
     <div> 
      <table> 
       <thead> 
        <tr> 
         <th> 
          Name 
         </th> 
         <th> 
          Value 
         </th> 
         <th> 
          Comment 
         </th> 
         <th> 
          Action 
         </th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr ng-repeat="resource in resources"> 
         <td> 
          {{resource.KeyName}} 
         </td> 
         <td> 
          {{resource.Text}} 
         </td> 
         <td> 
          {{resource.Comment}} 
         </td> 
         <td> 
          <span>Update</span> 
         </td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 
     </form> 
    </div> 
</body> 
</html> 

這是我的JS:

var myModule = angular.module('Resource', []).factory('ResourceData', function($http) { 
    return { 
     getResources: function(prmLangId) { 
      //return the promise directly. 
      return $http.get('Default.aspx/GetResources', { langId: prmLangId }). 
         then(function(result) { 
          //resolve the promise as the data 
          console.log(result); 
          return result.data; 
         }); 
     } 
    } 
}); 

myModule.config(function($httpProvider) { 

      $httpProvider.defaults.headers.get = {}; 

      $httpProvider.defaults.headers.get["Content-Type"] = "application/json; charset=utf-8"; 

     }); 

function ResourcesCtrl($scope, ResourceData) { 
    $scope.resources = ResourceData.getResources(1033); 
} 

這是服務器方法:

[WebMethod] 
    public static string GetResources(int langId) 
    { 
     ResourcesManagerBL resBl = new ResourcesManagerBL(langId); 
     var resources = resBl.GetResourcesForLanguage(langId); 
     return Json.ToJson(resources); 
    } 

結果是許多空行(作爲資源的數量), 我在做什麼錯,在控制檯中數據看起來OK。

結果是這樣的:

enter image description here

+0

變化來填充parm數據類型int到字符串 –

+0

@RameshRajendran,它沒有幫助.. – ronen

+1

如果結果包含html而不是json字符串,那麼該方法沒有正確命中。嘗試通過設置適當的標題並查看是否得到json結果,使用諸如fiddler或'Postman(鉻工具)之類的工具來訪問url Default.aspx/GetResources/1033'。 – Chandermani

回答

0

Web方法可以返回的實際類型(如表),並模塊應該result.data.d

相關問題