我设置了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));
}
}
然而,当我通过$ 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;
})
});
我在控制台上收到此错误:
如果我点击错误信息中的链接(上图中带下划线),我也会在页面上看到错误:
请记住:我在浏览器上手动输入URL时可以访问JSON。
答案 0 :(得分:0)
[System.Web.Script.Services.ScriptService]//add it and try again
public class ExpenseService : System.Web.Services.WebService