Tuesday, October 15, 2013

sending data from jquery to webservice | calling web service from jquery on ajax/json post in asp.net

hi in this post i will show how to call a webservice from jquery using ajax/Json post in asp.net :


1. add in web.config

    <authorization>
      <allow users="*"/>
    </authorization>
    

2. add a webservice file and in codebehind of it add method to be called on ajax-json post request:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]

public class WebService : System.Web.Services.WebService {

    public WebService () {
       
    }

    [WebMethod]
    public string HelloWorld(string message, string Type)
    {
        return "Hello World";
    }
    
}


3.   Jquery code to be used on the html page to call a webservice on ajax/json post request :


   var Tag = '{message: "' + name.val().toString() + '", Type: "' + 'A' + '"}';

                            $.ajax({ type: "POST", url: "WebService.asmx/HelloWorld", cache: false, contentType: "application/json; charset=utf-8", data: Tag, dataType: "json",
                                success: function (msg) {
                                   
                                    alert('Successful');

                                },

                                error: function (response) {

                                    alert(response);

                                }

                            });

No comments:

Post a Comment