Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理这个jquery ajax请求的webmethod的正确签名是什么?_Jquery_Asp.net_Ajax_Webmethod - Fatal编程技术网

处理这个jquery ajax请求的webmethod的正确签名是什么?

处理这个jquery ajax请求的webmethod的正确签名是什么?,jquery,asp.net,ajax,webmethod,Jquery,Asp.net,Ajax,Webmethod,我的webmethod需要什么签名才能将“paramlist”作为参数传递 <script type="text/javascript"> $(document).ready(function () { var slider = $('.slider').slider({ range: "min", min: 0, max: 100, change: functio

我的webmethod需要什么签名才能将“paramlist”作为参数传递

<script type="text/javascript">
    $(document).ready(function () {
        var slider = $('.slider').slider({
            range: "min",
            min: 0,
            max: 100,
            change: function (e, ui) {
                var set = new Array();

                var values = $('.slider').each(function () {
                    var s = $(this);
                    var data = {
                        Name: s.attr('itemName'),
                        SelectedIndex: s.slider("option","value"),
                        Description: "this is the description",
                        CalculatedValue: 0
                    }

                    set.push(data);    
                });

                CallPageMethod("SliderChanged", set, successful, failure);
            },
            slide: function (e, ui) {
                var point = ui.value;
                $("#selected_value").html(point);
                // var width = 100 - point;
                // $("#range").css({ "width": point + "%" });
            }
        });

        function CallPageMethod(methodName, paramArray, onSuccess, onFail) {
            //get the current location
            var loc = window.location.href;
            loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;

            //call the page method
            $.ajax({
                type: "POST",
                url: loc + "/" + methodName,
                data:  JSON.stringify(paramArray),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: onSuccess,
                fail: onFail
            });
        }

        function successful(response) {
            var lbl = $('#<%= Label1.ClientID %>')
            lbl.html("Your report is now ready for download.");
            alert(response.d);
        }

        function failure(response) {
            alert("An error occurred.");
        }
    });
</script> 
在哪里

public class MyModel
{
   public string Name {get;set;}
   public string Description {get;set;}
   public int SelectedIndex{get;set;}
   public int CalculatedValue(get;set;}
}
但它失败了


你能发现我的错误吗?

好的,让我们简化一下,因为那些字符串串联很难看。一旦在服务器上复制相同的结构,就很容易实现这一点

因此,假设您希望将以下javascript对象发送到服务器:

var myObject = { a: { one: 1, two: 2 }, b: [1, 2, 3] };
您将定义与此签名匹配的类:

public class MyModel
{
    public Foo A { get; set; }
    public int[] B { get; set; }
}

public class Foo
{
    public int One { get; set; }
    public int Two { get; set; }
}
并有一个webmethod:

[WebMethod]
public string SomeMethod(MyModel model)
{
    ...
}
您会这样调用它:

var myObject = { a: { one: 1, two: 2 }, b: [1, 2, 3] };
$.ajax({
    url: '/SomeService.asmx/SomeMethod',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(myObject),
    success: function(result) {
        // Notice the .d property here. That's specific to ASP.NET web methods, 
        // which wrap the response using this property, like this:
        // { d: ... }
        alert(result.d);
    }
});
var myObject = [
    { a: { one: 1, two: 2 }, b: [1, 2, 3] },
    { a: { one: 5, two: 9 }, b: [7, 3, 4] },
    { a: { one: 3, two: 0 }, b: [3, 9, 3] },
]
[WebMethod]
public string SomeMethod(MyModel[] model)
{
    ...
}
注意
JSON.stringify
方法。它将javascript对象转换为JSON字符串表示形式。此方法本机内置于现代web浏览器中,但如果您需要支持传统浏览器,您可以在页面中包含此方法,以测试浏览器是否本机支持此方法并使用它,或者它是否不提供替代实现

如果要发送对象数组,请执行以下操作:

var myObject = { a: { one: 1, two: 2 }, b: [1, 2, 3] };
$.ajax({
    url: '/SomeService.asmx/SomeMethod',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(myObject),
    success: function(result) {
        // Notice the .d property here. That's specific to ASP.NET web methods, 
        // which wrap the response using this property, like this:
        // { d: ... }
        alert(result.d);
    }
});
var myObject = [
    { a: { one: 1, two: 2 }, b: [1, 2, 3] },
    { a: { one: 5, two: 9 }, b: [7, 3, 4] },
    { a: { one: 3, two: 0 }, b: [3, 9, 3] },
]
[WebMethod]
public string SomeMethod(MyModel[] model)
{
    ...
}
然后,您的web方法将如下所示:

var myObject = { a: { one: 1, two: 2 }, b: [1, 2, 3] };
$.ajax({
    url: '/SomeService.asmx/SomeMethod',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(myObject),
    success: function(result) {
        // Notice the .d property here. That's specific to ASP.NET web methods, 
        // which wrap the response using this property, like this:
        // { d: ... }
        alert(result.d);
    }
});
var myObject = [
    { a: { one: 1, two: 2 }, b: [1, 2, 3] },
    { a: { one: 5, two: 9 }, b: [7, 3, 4] },
    { a: { one: 3, two: 0 }, b: [3, 9, 3] },
]
[WebMethod]
public string SomeMethod(MyModel[] model)
{
    ...
}

有了这些知识,您可以非常轻松地在javascript和web方法之间交换数据结构。

Hi Darin-这让我走上了正确的道路。我发送到webmethod的类数组仍然存在问题。我上面所做的有意义吗(按照你的建议更改代码)?我仍然收到一个关于“不支持反序列化数组”的错误