【问题标题】:How to call WebMethod inside Class file using Angularjs?如何使用 Angularjs 在 Class 文件中调用 WebMethod?
【发布时间】:2016-04-06 07:41:05
【问题描述】:
我正在使用angularjs 呼叫WebMethod。当WebMethod 在 .aspx.cs 中时我得到了响应,但如果WebMethod 在 class 文件(class1.cs) 中没有得到响应。
调用返回403 Forbidden错误。
类文件::
using System.Web.Services;
using System.IO;
public class done
{
[WebMethod]
public static string test()
{
string x = File.ReadAllText(@"C:/Users/Admin/Desktop/live.json");
return x;
}
}
类文件中的WebMethod如何获取响应。
【问题讨论】:
标签:
c#
angularjs
webmethod
【解决方案1】:
我可能是错的,但我在 .cs 文件中使用 WebMethod 时遇到了问题,因为它通常用于 aspx.cs 文件。相反,我在 .cs 文件中使用了 HttpPost 属性。我没有使用过 AngularJS(这里是 jQuery 人),但我可以想象它会有同样的问题。如果您仍然需要帮助,这就是我解决错误的方法。
CS 控制器
[HttpPost]
public ReturnType MethodName(ParamType param1, ParamType param1)
{
obj = new ReturnType();
// do something
return obj;
}
Ajax 调用
$.ajax({
type: 'POST',
url: "@Url.Action("MethodName", "Contoller")",
//if not MVC, just <path to file>/MethodName
error: function () {
console.log("=(");
},
success: function () {
console.log("hi");
}
})
希望对您有所帮助!