从AngularJS控制器调用时,ASP.NET Web Service返回代码500

时间:2016-09-08 01:38:55

标签: javascript c# asp.net angularjs web-services

我设置了AMSX Web服务,并且在访问URL时它可以正常工作:

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

[WebMethod]
    public void Get()
    {
        List<Expense> expenses = new List<Expense>();
        expenses.Add(new Expense(1, "Netflix", "Netflix", 20.00, new DateTime()));
        expenses.Add(new Expense(2, "Spotify", "Spotify", 14.90, new DateTime()));
        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(expenses));      
    }
}

Success

然而,当我通过$ http:

从我的AngularJS控制器进行GET时
var app = angular.module("app", ["ngRoute"]);

app.config(function ($routeProvider) {
    $routeProvider.when("/home", {
        templateUrl: "Pages/Home.html",
        controller: "homeController"
    })
});

app.controller("homeController", function ($http) {
var self = this;
$http.get("ExpenseService.asmx/Get")
     .then(function (response) {
         self.expenses = response.data;
     })
});

我在控制台上收到此错误:

Console Error

如果我点击错误信息中的链接(上图中带下划线),我也会在页面上看到错误:

Error on page

请记住:我在浏览器上手动输入URL时可以访问JSON。

1 个答案:

答案 0 :(得分:0)

[System.Web.Script.Services.ScriptService]//add it and try again 
public class ExpenseService : System.Web.Services.WebService