开发者

how to use jquery to send a parameter array to an asp.net web method

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";


    }


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

data: paramList,

Should be:

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


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 {

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜