我刚刚将VS 2010,MVC 2,jQuery 1.7应用程序升级到VS 2012,MVC 5和jQuery 1.10。
该应用使用MicrosoftAjax.js和MicrosoftMvcAjax.js。
我发布了一个表单,操作返回了一个json结果。为此,我在客户端收到以下错误:
TypeError: context.get_data is not a function
var json = context.get_data();
TypeError: context.get_object is not a function
var json = context.get_object().get_data();
请注意原始代码使用的是context.get_data()。错误后我将其更改为context.get_object()。get_data()。
我还尝试将json结果编码如下,但这仍然导致相同的错误:
public JsonResult AddJsonUtf8Encoding(JsonResult result)
{
result.ContentEncoding = System.Text.Encoding.UTF8;
result.ContentType = "application/json; charset=UTF-8";
return result;
}
页面上的javascript:
var MvcTopBarLogin =
{
beginAjaxForm: function () {
$('#msgboxSignInTopBar').removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
},
successAjaxForm: function (context) {
**var json = context.get_object().get_data();**
//var json = context.get_data();
var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);
if (data.IsError) {
if (data.IsGone) {
window.location.href = data.RedirectUrl;
}
else {
$('#msgboxSignInTopBar').fadeTo(200, 0.1, function () {
$(this).html(data.Message).addClass('messageboxerror').fadeTo(900, 1).delay(3000).fadeOut(1000);
});
}
}
else {
if (data.RedirectUrl == "")
window.location.reload();
else
window.location.href = data.RedirectUrl;
}
}
}
HTML:
<% using (Ajax.BeginForm(ActionNames.ValidateUser,
ControllerNames.Account,
new { Area = "" },
new AjaxOptions
{
HttpMethod = "Post",
OnBegin = "MvcTopBarLogin.beginAjaxForm",
OnSuccess = "MvcTopBarLogin.successAjaxForm"
}, new { id = "loginForm" }))
{ %>
<%= Html.AntiForgeryToken() %>
<%= Html.HiddenFor(x => x.RawUrl) %>
<div id="signIn">
<input type="image" src="<%= Url.Image("/Structure/Buttons/btn_signIn_topBar.gif") %>" class="ntptEventTag" ntptEventTag="TopBox-MVC-Login" />
</div>
<div id="login_box">
<label for="Password" class="overlabel">
Password</label>
<%= Html.PasswordFor(x => x.Password, new { @class = "textBox swap_value", tabIndex = 2 })%>
</div>
<div id="login_box">
<label for="Username" class="overlabel">
Username</label>
<%= Html.TextBoxFor(x => x.Username, new { @class = "textBox swap_value", tabIndex = 1 })%>
</div>
<%
} %>
谢谢你的期待。
答案 0 :(得分:1)
这适用于编码:
var MvcTopBarLogin =
{
beginAjaxForm: function () {
$('#msgboxSignInTopBar').removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
},
successAjaxForm: function (context) {
//var json = context.get_object().get_data();
//var json = context.get_data();
//var json = context.get_response().get_object().get_data();
var data = context; //Sys.Serialization.JavaScriptSerializer.deserialize(json);
if (data.IsError) {
if (data.IsGone) {
window.location.href = data.RedirectUrl;
}
else {
$('#msgboxSignInTopBar').fadeTo(200, 0.1, function () {
$(this).html(data.Message).addClass('messageboxerror').fadeTo(900, 1).delay(3000).fadeOut(1000);
});
}
}
else {
if (data.RedirectUrl == "")
window.location.reload();
else
window.location.href = data.RedirectUrl;
}
}
};