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

试试这个网站,它对你有用..

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 回答
0

此参数名称有问题,请在您的方法中更改参数名称

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

并使方法静态,更新代码在上面

于 2012-11-08T11:42:27.877 回答