2012-01-26 21 views
1

我開發在asp.net應用程序中,我有jQuery代碼在我的ASP頁面成功消息在阿賈克斯jQuery的

var strdata = { 
      problemID: $("#problemID").val(), 
      commentText: $("#_1").val(), 
      empID: $("#empID").val(), 
      agree: 0, 
      disagree: 0 
     }; 
     $.ajax({ 
      type: "POST", 
      url: "<%= Url.Action("PostComment", "Discussion") %>", 
      data: strdata, 
      dataType: "JSON", 
      success: function (msg) { 
       if (msg == 1) 
       alert("Success" + msg); 
      } 
     }); 

和我的控制器具有代碼

public bool PostComment(string problemID, string commentText, string empID, string agree, string disagree) 
     { 


      return _discussionRepository.postComment(Convert.ToInt32(problemID), commentText, Convert.ToInt32(empID), Convert.ToInt32(agree),Convert.ToInt32(disagree)); 
     } 

和模型代碼

public bool postComment(int problemID, string commentText, int empID, int agree, int disagree) 
     { 
      bool result = false; 
      Comment c = new Comment(); 
      c.ProblemID = problemID; 
      c.CommentText = commentText; 
      c.EmpID = empID; 
      c.Agree = agree; 
      c.DisAgree = disagree; 

      _objectModel.Comments.InsertOnSubmit(c); 
      try 
      { 
       _objectModel.SubmitChanges(); 
       result = true; 

      } 

      catch (Exception ex) 
      { 
      } 


      return result; 

} 

數據在數據庫中通過Ajax和jQuery節省,但成功的消息沒有顯示

回答

1

如果警報未用或不用,這意味着該數據類型返回的狀態下運行是不是$就功能期待的數據類型。

2種方式得到它的底部:

  • 首先打開瀏覽器或螢火蟲,並檢查了網絡流量。如果你得到的結果(請求正在做,內容看起來準確),那麼你的數據類型肯定是原因。嘗試更改請求中的數據類型。

  • 接下來,你可以嘗試增加其他功能不僅僅是成功。還有錯誤,狀態代碼(404,500等),BeforeSend等檢查文檔。

還有其他方法:)。提琴手也可能會提供幫助。

+0

我得到了一個錯誤? [目標對象] – Snake

+0

你的數據類型的錯誤是「JSON」,但我沒有看到你輸出JSON嘗試用你的包裹return語句:VAR jsonSerializer =新System.Web.Script.Serialization.JavaScriptSerializer(); JSON字符串= jsonSerializer.Serialize(yourCustomObject); 更多的信息在這裏:http://stackoverflow.com/questions/2287399/encode-object-to-json 此外,您可能需要設置頁面類型的標題爲「應用程序/ json「...嘗試先不使用json? – Parris

0

我想結果在「味精」心不是數字1。也許味精==真的回來了?

+0

我已經運行沒有這個條件這個代碼,但警報不顯示。 – Snake

0

爲了讓您的AJAX請求使用下面的代碼在你的成功處理程序的結果:

success: function (msg) { 
      if (msg.d === true) { 
       alert("Success" + msg.d); 
      } 
     } 

你必須通過「d」屬性來訪問結果。

爲了得到錯誤信息使用下面的錯誤處理程序:

error: function(jqXHR, textStatus, errorThrown){ 
     alert(errorThrown); 
     } 
+0

還是它不工作:( – Snake

+0

@Snake:你有沒有嘗試使用錯誤處理程序在你的jQuery AJAX調用代碼/錯誤消息你什麼錯誤 – Hans

+0

我得到的翻譯: – Snake