【发布时间】:2015-03-31 22:52:16
【问题描述】:
我花了一天多的时间试图获取我主页的 jquery,以访问我在 C# ASP.NET MVC 项目中拥有的 api 控制器。
APP_START中的RouteConfig.cs是这样的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "API Default",
url: "api/{controller}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}
我的类名为 LogInController,位于 Controllers 目录中:
我已经将 LogInController 类设置为:
这是我在 Index.cshtml 上的 jquery:
var apiUrl = "api/LogIn";
-
$.post(apiUrl,
{
userName: 'kamron',
pwHash: sha256_digest('password')
},
function (data, status) {
try {
alert(data, status);
} catch (e) {
alert("System error, please try again.");
};
});
和
$.getJSON(apiUrl)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
alert(key + ':' + item);
});
});
我将不胜感激。我已经盯着这个看了一天多,试图弄清楚我的问题是什么......
任何对我有帮助的额外信息请索取,我已尝试发布所有相关信息。
非常感谢大家:)
【问题讨论】:
-
把你
API Default路由放在Default路由之前 -
好的,这样做了,但在 GET 和 POST 上仍然得到 404
-
试试
var apiUrl = "/api/LogIn"; -
不走运:/感谢您的帮助。在 MVC 项目中定义新的 ApiController 时,有什么地方我需要做的吗?我在 Web.config 中没有看到任何相关的内容
-
抱歉,我没有仔细阅读您的代码 - this answer 可能会有所帮助
标签: c# jquery asp.net-mvc api controller