Reputation: 3174
I just finished upgrading VS 2010, MVC 2, jQuery 1.7 app to VS 2012, MVC 5 and jQuery 1.10.
The app uses MicrosoftAjax.js and MicrosoftMvcAjax.js.
I am posting a form and the action is returning a json result. For this i am getting the following error on the client side:
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();
Please note that original code was using context.get_data(). After the error i changed it to context.get_object().get_data().
I have also tried encoding the json result as following but this is still resulting in the same error:
public JsonResult AddJsonUtf8Encoding(JsonResult result)
{
result.ContentEncoding = System.Text.Encoding.UTF8;
result.ContentType = "application/json; charset=UTF-8";
return result;
}
javascript on page:
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>
<%
} %>
Thanks for looking.
Upvotes: 1
Views: 305
Reputation: 3174
This is working with encoding:
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;
}
}
};
Upvotes: 1