0

私は次のJavaScriptを持っています:

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: "WebForm3.aspx/sayHello",
            contentType: "application/json; charset=utf-8",
            data: "{}",
            dataType: "json",
            success: AjaxSucceeded,
            error: AjaxFailed
        }); 
    });
    function AjaxSucceeded(result) {
        alert(result.d);
    }
    function AjaxFailed(result) {
        alert(result.status + ' ' + result.statusText);
    }  
</script>  

私のコードビハインドには次のものがあります。

[WebMethod]
public static string sayHello() 
{
    return "hello";
}

これは機能し、こんにちはを示します。

今、もしそうなら:

[WebMethod]
public static string sayHello(string args) 
{
    return "hello";
}

その後、応答として内部サーバー エラー 500 が返されます。

これを変更して実際のデータをサーバー側に送信できるようにするにはどうすればよいですか?

4

2 に答える 2

5
data: {
    args: "hello world"
},

contentTypeまた、両方とも削除dataTypeします。
また、 を追加してくださいtraditional: true。結果のリクエストは次のようになります。

$.ajax({
    type: "POST",
    url: "WebForm3.aspx/sayHello",
    traditional: true,
    data: {
        args: "hello world"
    },
    success: AjaxSucceeded,
    error: AjaxFailed
}); 
于 2013-01-31T18:50:14.093 に答える
1

良い、

これは機能します:

  $(document).ready(function() {

      $.ajax({
          type: "POST",
          url: "WebForm3.aspx/Test",
          data: '{"data":"abc"}',
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (response) {
              alert(response.d);
          }
      });
});
于 2013-01-31T19:18:37.497 に答える