2013-12-13 69 views
1

我刚刚完成升级VS 2010,MVC 2,jQuery 1.7应用到VS 2012,MVC 5和jQuery 1.10。Json返回问题

该应用使用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结果如下但这仍然导致了同样的错误:

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> 
    <% 
     } %> 

public JsonResult AddJsonUtf8Encoding(JsonResult result) 
     { 
      result.ContentEncoding = System.Text.Encoding.UTF8; 
      result.ContentType = "application/json; charset=UTF-8"; 
      return result; 
     } 

页上的JavaScript

感谢您的期待。

+0

我还检查 “context.get_response()的get_object();”。这导致了同样的错误。 –

回答

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; 
     } 
    }  
};