How to pass Parameter to a web method using json

Monday, 29 December 2014

How to pass Parameter to a web method using json

1. Write down this json function with in the aspx page 

 function bind() {
            $.ajax({
                type: "POST",
                url: "Mypage.aspx/WebMethodname",
                data: '{"Parameter1":"' + p1  + '","Parameter2":"' + p2  + '"}',
                contentType: "application/json",
                dataType: "json",
                success: ServiceSucceeded,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (result) {
                    alert('Service call failed: ' + result.status + ' ' + result.statusText);
                    // varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;
                }
            })
        }

      function ServiceSucceeded(result) {//When service call is sucessful
            // alert("Service Result: " + JSON.stringify(result.d));// For Service Result type JSON
            document.getElementById('<%=Lbl_finMessage.ClientID%>').innerHTML = JSON.stringify(result.d);
        }

2. Write the following function with in the web method of aspx page :(Mypage.aspx)
name of the parameter of WebMethodname and json call parameter name will be same
  <WebMethod()>
    Public Shared Function WebMethodname(ByVal Parameter1 As String, ByVal Parameter2As String) As String

   
    End Function