如何使用jquery将参数数组发送到asp.net web方法

发布于 12-05 08:46 字数 3003 浏览 0 评论 0原文

使用下面的 jquery,我尝试将数组 paramlist 提交到下面适当的 Web 方法。我在这里缺少什么?

 <script type="text/javascript">
        $(document).ready(function () {

            var slider = $('.slider').slider({
                range: "min",
                min: 0,
                max: 100,
                change: function (e, ui) {

                    var paramList = new Array();

                    var values = $('.slider').each(function () {
                        var s = $(this);
                        var aType = s.attr('itemName');
                        var point = s.slider("option", "value");
                        paramList.push(aType);
                        paramList.push(point);


                    });

                    CallPageMethod("SliderChanged", paramList, success, fail);
                    //                    $("#img1").fadeOut();
                    //                    alert("done");
                },
                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) {

                //create list of parameters in the form
                //{"paramName1":"paramValue1","paramName2":"paramValue2"}

                var paramList = '';
                if (paramArray.length > 0) {
                    for (var i = 0; i < paramArray.length; i += 2) {
                        if (paramList.length > 0) paramList += ",";
                        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
                    }
                }
                paramList = '{' + paramList + '}';


                //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: paramList,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: onSuccess,
                    fail: onFail

                });

            }


            function success(response) {

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

            }

            function fail(response) {
                alert("An error occurred.");
            }

        });


   </script>

我有以下网络方法:

  [WebMethod]
    public static string SliderChanged(string[] values)
    {

        return "successArray";


    }

using the jquery below I'm trying to submit the array paramlist to the appropriate web method below. What am I missing here?

 <script type="text/javascript">
        $(document).ready(function () {

            var slider = $('.slider').slider({
                range: "min",
                min: 0,
                max: 100,
                change: function (e, ui) {

                    var paramList = new Array();

                    var values = $('.slider').each(function () {
                        var s = $(this);
                        var aType = s.attr('itemName');
                        var point = s.slider("option", "value");
                        paramList.push(aType);
                        paramList.push(point);


                    });

                    CallPageMethod("SliderChanged", paramList, success, fail);
                    //                    $("#img1").fadeOut();
                    //                    alert("done");
                },
                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) {

                //create list of parameters in the form
                //{"paramName1":"paramValue1","paramName2":"paramValue2"}

                var paramList = '';
                if (paramArray.length > 0) {
                    for (var i = 0; i < paramArray.length; i += 2) {
                        if (paramList.length > 0) paramList += ",";
                        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
                    }
                }
                paramList = '{' + paramList + '}';


                //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: paramList,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: onSuccess,
                    fail: onFail

                });

            }


            function success(response) {

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

            }

            function fail(response) {
                alert("An error occurred.");
            }

        });


   </script>

I have the following web methods:

  [WebMethod]
    public static string SliderChanged(string[] values)
    {

        return "successArray";


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年华零落成诗2024-12-12 08:46:11

正如其他人所说,您需要指定参数名称;因此,

data: paramList,

应该是:

data: '{ "values":'+paramList+'}',

As other people have said, you need to specify the parameter name; therefore,

data: paramList,

Should be:

data: '{ "values":'+paramList+'}',
屋檐2024-12-12 08:46:11

一些观察:

  1. 通常您的 Web 服务以 .asmx 文件形式提供。
    因此,在您的 ajax 调用中,您需要提供它的位置,而不是 .aspx 页面。
    例如 url: "/WebService.asmx/" + methodName,

  2. 确保可以从 Javascript 访问您的 Web 服务。
    为此,请装饰类:
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {

A few observations:

  1. Usually your Webservice is provided as an .asmx file.
    Therefor in your ajax-call you want to supply it's location, not an .aspx-page.
    E.g. url: "/WebService.asmx/" + methodName,

  2. Make sure that your webservice can be accessed from Javascript.
    To do so decorate the class:
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService {

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文