2013-12-13 89 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; 
     } 
    }  
};