我通过将数据通过FormData()发送到后端中的操作来登录系统中的用户。用户登录,一切都很好。我想将响应(字符串,Json)发送回前端。
[Route("login")]
[HttpPost]
public async Task<ActionResult> LoginAsync(string username, string password)
{
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
var result = await mSignInManager.PasswordSignInAsync(username, password, true, false);
if (result.Succeeded)
{
return new JsonResult("Succeeded", new JsonSerializerSettings());
// or return new[] { user.DisplayName, user.Role };
}
return new JsonResult("Failed", new JsonSerializerSettings());
}
在前端,我希望看到传递的字符串或json,但我将其作为获取的响应。
Response {type: "basic", url: "https://ip/api/sampledata/login", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "https://ip/api/sampledata/login"
答案 0 :(得分:2)
您可以只使用ContentResult
返回纯字符串:
public async Task<ActionResult> LoginAsync(string username, string password)
{
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
var result = await mSignInManager.PasswordSignInAsync(username, password, true, false);
if (result.Succeeded)
{
return new JsonResult("Succeeded", new JsonSerializerSettings());
// or return new[] { user.DisplayName, user.Role };
}
return Content("Failed");
}
ContentResult
默认返回一个text/plain
作为其 contentType 。