0

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


    }
3
  • I didn't think you could do function overloading with webmethods without specifying the message name. Can you explain the error you're getting better so people don't have to make a test solution for this? Source: msdn.microsoft.com/en-us/library/… Commented Sep 16, 2011 at 15:07
  • I agree, as far as I know, and last I checked, you cannot overload web method names, as they are generated into a soap request that doesn't have the ability to distinguish overloads by parameter type alone. Commented Sep 16, 2011 at 15:10
  • Ok - I only have the one method (no longer an overload). I set breakpoints in visual studio and neither the webmethod or the success or fail methods are hit. Commented Sep 16, 2011 at 15:32

2 Answers 2

1

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

data: paramList,

Should be:

data: '{ "values":'+paramList+'}',
Sign up to request clarification or add additional context in comments.

Comments

1

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 {

1 Comment

Although that wasn't my issue, this will help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.