2012-01-26 12 views
1

私は私が私のASPページに成功メッセージ

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

をjqueryのコードを持っているasp.netでアプリを開発していますし、私のコントローラは、コード

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 jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string json = jsonSerializer.Serialize(yourCustomObject); さらに詳しい情報はこちら:http://stackoverflow.com/questions/2287399/encode-object-to-json ページタイプのヘッダーを「application」に設定する必要がある場合もあります/ json "...おそらくjsonを最初に使ってみませんか? – Parris

0

'msg'に返される結果は1ではないと思います。多分msg == true?

+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

関連する問題