none
Ajax -> web service call help request RRS feed

  • Question

  • Greetings,
    I've been wrestling with this problem for a couple of days now.  I just can't figure out how to send parameters to a web service that I've written.  I'm using the JSGrid library - it provides the filter value below.  Relevant code is below.  I don't get an error message, it just appears that the filter string isn't being received by the web service.

    Web Page
    ==========
            function loadRecords2(filter) {
                var d = $.Deferred();
    // Browser inspect shows that {filter = {FirstName: "", LastName: "Smith"} which is correct
                var myFilter = JSON.stringify({ filter });
                $.ajax({
                    type: "POST",
                    url: "service1.asmx/getPeople2",
                    data: myFilter,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                })
                .error(function (request, status, error) {alert(request.responseText);})
                .done(function (response) { d.resolve(response.d); })
                return d.promise();
            }

    Web Service
    =============
    [WebMethod]

    public List<People> getPeople2(List<String> filter)  // runs, but filter.Count always = 0  :(
    //public List<People> getPeople2(List<FilterTerms> filter) // also runs, but filter.Count always = 0  :(

    {
    string sql = "Select * FROM tblPeople where deleted = False";
    [do more stuff]
    }

    public class FilterTerms
    {
    public String[] FirstName { get; set; }
    public String[] LastName { get; set; }
    }
        
    Tuesday, November 26, 2019 7:32 PM

All replies

  • Hi,
    By default, XML web service use wrapped message body. It means that we should wrap the parameter name in the request body.
    Please refer to the below example.
    WebService1.asmx

        public class FilterTerms
        {
            public String[] FirstName { get; set; }
            public String[] LastName { get; set; }
    }
            [WebMethod]
            public string getPeople2(List<FilterTerms> filter)
            {
                return "ABCD";
            }


    Webform2.aspx

        <script src="Scripts/jquery-3.4.1.js"></script>
    <script>
    //use array to represent List<T>
            var filter = {
                "filter": [{ "FirstName": ["A","B"], "LastName": ["Abraham","Pear"] }, { "FirstName": ["A","C"], "LastName": ["Abraham2"] }]
                    };
            var jsonstring = JSON.stringify(filter);
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/getPeople2",
                data: jsonstring,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessCall,
                error: OnErrorCall
            });
    
            function OnSuccessCall(response) {
                console.log(response.d);
            }
    
            function OnErrorCall(response) {
                console.log(response.status + " " + response.statusText);
            }
        </script>

    Result.

    https://i.stack.imgur.com/OvTbH.png

    Feel free to let me know if there is anything I can help with.

    Best Regards

    Abraham


    Wednesday, November 27, 2019 8:46 AM
    Moderator
  • Thank you.  This helped me figure out the issue.  The correct signature is public List<People> getPeople2(FilterTerms filter).  I was expecting a list, but ajax wasn't sending one.  Thanks again.
    Wednesday, November 27, 2019 2:50 PM
  • Hi,
    If the signature of the memthod is 
    public List<People> getPeople2(FilterTerms filter)


    We can set up it by the below JS object.
            var filter = {
                "filter": { "FirstName": ["A","B"], "LastName": ["Abraham","Pear"] }
                    };
            var jsonstring = JSON.stringify(filter);
            $.ajax({
                type: "POST",
                url: "WebService1.asmx/getPeople2",
                data: jsonstring,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccessCall,
                error: OnErrorCall
            });
    

    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham
    Thursday, November 28, 2019 2:17 AM
    Moderator