【发布时间】:2017-03-30 20:56:58
【问题描述】:
我正在尝试将 AngularJS 与 NET Core 结合使用。目前正在尝试解析“字符串用户名”、“字符串密码”和“bool rememberMe”,但无论我做什么,这两个字符串都是空的。我什至尝试解析如下所示的常量值,即参数“test”,但即使这样也不起作用。我已经在 Fiddler 中确认脚本实际上是在解析值,所以控制器基本上无法“获取它”。
下面是我的 AngularJS loginController 代码。
var login = angular.module('login', []);
login.controller('LoginController',
[
'$scope', '$http', '$window', function ($scope, $http, $window) {
$scope.submit = function () {
var data = { test: ':D' };
$http({
method: 'POST',
url: '/admin',
data: data
}).then(function success(response) {
alert(response.data.test);
});
}
}
]);
下面是我的 ActionResult 的代码,用于获取发布到它的值。
[HttpPost]
public IActionResult Index(string test)
{
return Json(new
{
test,
bla = "BLA"
});
//if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
//{
// if (UserSystem.Login(username, password, rememberMe, HttpContext))
// {
// HttpContext.Response.StatusCode = (int)HttpStatusCode.Accepted;
// return Json(new
// {
// returnUrl = "/admin/dashboard"
// });
// }
//}
//HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
//return Json(new
//{
// error = "Username and/or password do not match"
//});
}
我也尝试过指定 Content-Type,但也没有运气。无论哪种方式,在 Fiddler 中,请求似乎总是默认为 application/json。
我希望有人可以帮助我。谢谢!
【问题讨论】:
-
您确定向正确的端点发出请求吗? /admin 路由到什么?
-
我确定。正如您在 ActionResult 中看到的,它返回一个常量值“bla = "BLA",我可以很好地得到这个值。