Elaheh kamaliha
Elaheh kamaliha

Reputation: 803

call ASP.net web service from javascript client

I have a previously written client-server application(by another programmer). The client side is written with javascript. I need to write a new web service with ASP.net but I don't know how to call web methods. My sitation is the same described here Call web service from javascript but it doesn't have a clear answer. (just says use WCF, I don't know how to do it) (note that my client is a totally different project from the server, suppose it is written in eclipse) I want to know how I can call the HelloWorld method that is created by default in aa ASP.net web service, from a simple html code(including js)

Thanks, Ela

Upvotes: 1

Views: 3064

Answers (1)

Chamika Sandamal
Chamika Sandamal

Reputation: 24292

refer the following links wish will provide you complete guide on how to call web service using JavaScript/Asp.net Ajax or jQuery.

http://cmsnsoftware.blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html

http://cmsnsoftware.blogspot.com/2011/02/how-to-use-ajax-auto-complete-in-aspnet.html

sample code

if (window.XMLHttpRequest) {
    // for IE7+, Firefox, Chrome, Opera, Safari
    this.xmlhttp = new XMLHttpRequest();
}
else {
    // for IE6, IE5
    try {
        this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
        try {
            // older version of Msxml
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            this.xmlhttp = null;
        }
    }
}
xmlhttp.onreadystatechange = function() {
    /// <summary>
    /// Display server time when success
    /// </summary>
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // success Status
        alert(xmlhttp.responseText);
    }
}
this.xmlhttp.open("POST", "AjaxServer.asmx/WebMethodName", true);
this.xmlhttp.send();

Upvotes: 1

Related Questions