gpt4 book ai didi

asp.net-mvc - 将 Json 对象从 Controller 操作返回到 jQuery

转载 作者:行者123 更新时间:2023-12-03 22:00:31 26 4
gpt4 key购买 nike

我正在尝试让它正常工作(现在已经 2 天了)。我正在处理一个日志,我从 jQuery 调用 Controller 操作,向其传递一个 JSON 对象(利用 json2.js)并从 Controller 返回一个 Json 对象。我可以很好地调用该操作,但不能将响应放在我想要的位置,它只是打开一个新窗口,并将其打印在屏幕上:

{"Message":"Invalid username/password combination"}

URL 看起来像 http://localhost:13719/Account/LogOn,因此不是调用操作也不重新加载页面,而是将用户带到 Controller ,这不好.

现在对于一些代码,首先是 Controller 代码

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl = "")
{
if (ModelState.IsValid)
{
var login = ObjectFactory.GetInstance<IRepository<PhotographerLogin>>();

var user = login.FindOne(x => x.Login == model.Username && x.Pwd == model.Password);

if (user == null)
return Json(new FailedLoginViewModel { Message = "Invalid username/password combination" });
else
{
if (!string.IsNullOrEmpty(returnUrl))
return Redirect(returnUrl);
else
return RedirectToAction("Index", "Home");
}
}
return RedirectToAction("Index", "Home");
}

以及 jQuery 代码

$("#signin_submit").click(function () {
var login = getLogin();
$.ajax({
type: "POST",
url: "../Account/LogOn",
data: JSON.stringify(login),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
$("#message").text(xhr.statusText);
},
success: function (result) {

}
});
});

function getLogin() {
var un = $("#username").val();
var pwd = $("#password").val();
var rememberMe = $("#rememberme").val();

return (un == "") ? null : { Username: un, Password: pwd, RememberMe: rememberMe };
}

如果您还需要在此处查看实际的登录表单

<fieldset id="signin_menu">
<div>
<span id="message"></span>
</div>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { @id = "signin" }))
{%>

<% ViewContext.FormContext.ValidationSummaryId = "valLogOnContainer"; %>
<%= Html.LabelFor(m => m.Username) %>
<%= Html.TextBoxFor(m => m.Username, new { @class = "inputbox", @tabindex = "4", @id = "username" })%><%= Html.ValidationMessageFor(m => m.Username, "*")%>
<p>
<%= Html.LabelFor(m=>m.Password) %>
<%= Html.PasswordFor(m => m.Password, new { @class = "inputbox", @tabindex = "5", @id = "password" })%><%= Html.ValidationMessageFor(m => m.Password, "*")%>
</p>
<p class="remember">
<input id="signin_submit" value="Sign in" tabindex="6" type="submit"/>
<%= Html.CheckBoxFor(m => m.RememberMe, new { @class = "inputbox", @tabindex = "7", @id = "rememberme" })%>
<%= Html.LabelFor(m => m.RememberMe) %>
<p class="forgot"> <a href="#" id="forgot_password_link" title="Click here to reset your password.">Forgot your password?</a> </p>
<p class="forgot-username"> <a href="#" id="forgot_username_link" title="Fogot your login name? We can help with that">Forgot your username?</a> </p>
</p>
<%= Html.ValidationSummaryJQuery("Please fix the following errors.", new Dictionary<string, object> { { "id", "valLogOnContainer" } })%>
<% } %>
</fieldset>

登录表单加载到主页上

<% Html.RenderPartial("LogonControl");%>

不确定这是否与此有任何关系,但我想我会提到它。

编辑:登录表单的加载类似于 Twitter 登录,单击链接,表单将在 jQuery 和 CSS 的帮助下加载

最佳答案

您的操作签名将如下所示:

public virtual JsonResult ActionName()
{
var abcObj = new ABC{a=1,b=2};

return Json(abcObj);
}

关于asp.net-mvc - 将 Json 对象从 Controller 操作返回到 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4564341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com