Calling ASP.NET WebMethod with more than one paramaters using JSON

Suppose we have a web method in ASP.NET web form or webservice

 [WebMethod]   
 public void GetItems(string itemName, string itemDesc)
 {

 }
 Using jQuery, we are going to call this method using the code like this below. I am pre-suming there is a search button which we have assigned id #searchNow
 

 $(document).ready(function () {  
  $(‘#searchNow’).click(function () {
  $.ajax({
     type: “POST”,
     url: “SearchItems.aspx/GetItems”,
     data: “{‘itemName’:’book’,’itemDesc’:’toys’}”,
     contentType: “application/json; charset=utf-8”,
     dataType: “json”,
            success: function (msg) {
           
            }
        });
 });
  Our interest is how to pass parameters to the web method.
 In the snippet above the line


 data: “{‘itemName’:’book’,’itemDesc’:’toys’}”,


is used to pass parameters to the GetItems function. Please not that it must be a JSON compliant string

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.