0

パラメータを渡すJavaScript:

<script>    
    var str = "pears"
    $.ajax({
        url: 'WebService.asmx/HelloWorld', //'/WebService.asmx/HelloWorld',
        data: "{outputtype:'" + str + "'}",
        type: "POST",
        cache: false,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            var response = msg.d;
            alert(response);
        },
        error: function (xhr, status, error) {
            alert(error + "\n" + xhr + "\n" + status); //do something if there is an error
        }
    });

そしてウェブメソッド:

[WebMethod]
public string HelloWorld(String str)
{
    return str;
}

私はこれに丸一日を費やし、このメソッドにパラメーターを渡す無数のバリエーションを試しましたが、どれもうまくいきませんでした。私はさまざまなマシンで試し、webmethod を静的にしてみました。また、javascript から値を渡すさまざまな方法を試しました。

ただし、パラメーターを渡さない場合は機能します。

4

2 に答える 2

0

このパラメータ名に問題があります。メソッドのパラメータ名を変更してください

[WebMethod] public static string HelloWorld(String outputtype)
                           { return outputtype; }

また、メソッドを静的にします。更新コードは上記のとおりです。

于 2012-11-08T11:42:27.877 に答える
0

このサイトを試してみてください..

var str = "pears"
$.ajax({
         type: "POST",
         url: "WebService.asmx/HelloWorld",
         data: "{'strabc1': '1'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         async: true,
         cache: false,
         success: function (data) {                       
             alert(data.d);
             alert(data.d[0]);                 
         }
      })

サーバー側の方法

サーバー側のメソッドからコレクションを返したい場合は、以下のArrayListの戻りメソッドが役立ちます..,

[WebMethod]
public static ArrayList HelloWorld(string strabc1)
{
        ArrayList arr = new ArrayList();
        arr.Add("abc");
        arr.Add("123");
        arr.Add("fff");
        return arr;
 }

この記事を参照

于 2012-11-08T12:32:21.140 に答える