2016-07-08 18 views
0

私はHTMLフォームからデータを取得し、JSMオブジェクトに変換してWebMethodに送信します。AJAXを使用してJSONオブジェクトをWebMethodに送信

今、私は成功の応答を得ていると言いますが、WebMethodに正しく送信されていることを確認する方法がわかりません。私はVS2015でデバッグしようとしましたが、ブレークポイントに到達することはできません。

これをすべて正しく設定したことを確認するにはどうすればよいですか?

<form id="addForm" name="addForm"> 
    <input type="text" name="playername" id="playername" placeholder="Player"/> 
    <input type="text" name="points" id="points" placeholder="Points" /> 
    <input type="text" name="steals" id="steals" placeholder="Steals" /> 
    <input type="text" name="blocks" id="blocks" placeholder="Blocks" /> 
    <input type="text" name="assists" id="assists" placeholder="Assists" /> 
    <input type="text" name="mpg" id="mpg" placeholder="MPG" /> 
    <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" /> 
    <input type="text" name="threepct" id="3pct" placeholder="3 %" /> 
    <input type="button" value="add player" id="addbtn" name="addbtn" /> 
    </form> 

マイアヤックス

$("#addbtn").click(function() { 
       var form = JSON.stringify($("form").serializeArray()); 


       $.ajax({ 
        method: 'POST', 
        url: "players.aspx/addRow", 
        data: form, 
        success: function (data) { 
         alert('success'); 
        }, 
        error: function() { 
         alert('failure'); 
        } 
       }); 
       }); 

と私はデータ

[WebMethod] 
     public static void addRow(string form) 
     { 

      dynamic players = JObject.Parse(form); 
     } 

私が好きなのを送信しようとしているウェブ方法:ここで

は私のhtmlですプレイヤーにはどのデータがあるか(おそらくAJAXから偽陽性を得ている)。

答えて

0

data: "{form:" + form + "}", 

data: form, 

を交換し、値を使用して、変数formaddRow方法文字列を取得しているものを見るためにVS2015を使用してWebMethod内のデバッガを置きます。

+0

、画面の左側にある赤いストップサインのように、正しいですか?私はそれをしてきましたが、私はブレークポイントを打つことができませんでした。私はダイナミックなプレイヤーと一緒にラインに入れました。私は間違って何をしていますか? – hereswilson

+0

@hereswilson:[これをチェック](http://www.aspsnippets.com/Articles/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet。 aspx)。デバッガがどのように値を表示しているかを参照してください。 –

+0

また、あなたのajax呼び出しURLが実際であることを確認してください。ブラウザコンソールを開いて、ajaxコールをonclickにしているときにエラーが発生していないかどうかを確認します。 –

0

の背後にあるコード:

public partial class players : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    [System.Web.Services.WebMethod] 
    public static void addRow(Player player) 
    { 
     System.Diagnostics.Debugger.Break(); 
     //dynamic players = JObject.Parse(form); 
    } 
} 

public class Player 
{ 
    public string playername { get; set; } 
    public string points { get; set; } 
    public string steals { get; set; } 
    public string blocks { get; set; } 
    public string assists { get; set; } 
    public string mpg { get; set; } 
    public string shotpct { get; set; } 
    public string _3pct { get; set; } 
} 

.ASPX:

<head runat="server"> 
    <title></title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 

      $("#addbtn").click(function() { 
       var player = 
        { 
         "playername": $("#playername").val(), 
         "points": $("#points").val(), 
         "steals": $("#steals").val(), 
         "blocks": $("#blocks").val(), 
         "assists": $("#assists").val(), 
         "mpg": $("#mpg").val(), 
         "shotpct": $("#shotpct").val(), 
         "_3pct": $("#3pct").val(), 
        }; 

       $.ajax({ 
        type: "POST", 
        url: "players.aspx/addRow", 
        contentType: "application/json", 
        data: JSON.stringify({ player: player }), 
        success: function (data) { 
         alert('success'); 
        }, 
        error: function (errordata) { 
         alert('failure'); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <form id="addForm" name="addForm"> 
     <input type="text" name="playername" id="playername" placeholder="Player" /> 
     <input type="text" name="points" id="points" placeholder="Points" /> 
     <input type="text" name="steals" id="steals" placeholder="Steals" /> 
     <input type="text" name="blocks" id="blocks" placeholder="Blocks" /> 
     <input type="text" name="assists" id="assists" placeholder="Assists" /> 
     <input type="text" name="mpg" id="mpg" placeholder="MPG" /> 
     <input type="text" name="shotpct" id="shotpct" placeholder="Shot %" /> 
     <input type="text" name="threepct" id="3pct" placeholder="3 %" /> 
     <input type="button" value="add player" id="addbtn" name="addbtn" /> 
    </form> 
</body> 
+0

だから私はそのコードをHTMLページではなく.aspxページに置く必要がありますか?私はそれで少し混乱しています。 – hereswilson

+0

まだブレークポイントに当たっていません。何が起こっている? – hereswilson

関連する問題