我目前在asp.net mvc 4中工作。我寫了一個返回JSON對象的函數。如何使用mvc使用JSON對象?
這是函數(在我的控制器)
[HttpPost]
public ActionResult Login(string email, string password)
{
ViewData["Message"] = "None";
JSONLoginModel model = new JSONLoginModel();
Account account = _accountRepository.CheckLogin2(email, password);
if (account != null)
{
model.Email = email;
model.Password = password;
model.ChangePassword = account.ChangePasswordOnLogin;
}
return Json(model);
}
這是JSONLoginModel
[Serializable]
public class JSONLoginModel
{
public string Email { get; set; }
public string Password { get; set; }
public bool ChangePassword { get; set; }
}
我也寫了下面的jQuery代碼來捕捉和使用它
$.post("Home/Login", { email: email, password: password }, function (data) {
alert(data);
$.each(data, function (index, IntraNoviUser) {
if (IntraNoviUser !== undefined && IntraNoviUser !== null) {
alert('test1');
alert(IntraNoviUser.password, IntraNoviUser.email);
alert('test2');
}
});
});
一切都很好,直到我嘗試使用返回的結果。
我能得到什麼,從我的控制器背面有一個對象,它可以有3種選擇:
- 空
- 電子郵件+密碼填寫並
changePassword
是假 - 相同2,但
changePassword = true
問題是我的返回的JSON對象永遠不會被識別。 對此有何暗示?
'alert(data)'顯示什麼?你有沒有檢查過你是否返回有效的JSON(並且可以包含問題中返回的內容)?警報(數據)顯示[object Object]; –
其他警報顯示未定義。我會用我的JSONLoginModel更新我的問題 – whodares
我更感興趣的是看到返回給瀏覽器的實際JSON。 –