1

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

Community
  • 1
  • 1
Elaheh kamaliha
  • 753
  • 2
  • 8
  • 17

1 Answers1

1

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();
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • I can't use this code,this seems to be an .aspx, but I need simple html – Elaheh kamaliha Oct 30 '11 at 06:41
  • It's not aspx, it's plain old javascript. If you can't tell the difference, then this is probably way over your head. – tdammers Oct 30 '11 at 06:47
  • @Elahehkamaliha- for the above code you don't need to use asp.net. you can call asp.net webservice using above code inside html file. only thing you need to have all in same domain. – Chamika Sandamal Oct 30 '11 at 06:48
  • @Chamika Sandamal- I didn't mean the part you've copied. I checked the link you have posted, and I guess the javascript sample is a .aspx file(it's name is default.aspx) – Elaheh kamaliha Oct 30 '11 at 08:57
  • @Elahehkamaliha- yes sample is an .aspx file. but you dont need .aspx file for this sample. that link shows the same task using three different methods. thats why it is in .aspx. so you can simply use it in HTML file. – Chamika Sandamal Oct 30 '11 at 09:41
  • I see. but I don't want to use the client in the same solution with the server. In this project, default.aspx(or .html) is part of the same project that has the server in it. – Elaheh kamaliha Oct 30 '11 at 10:14
  • @Elahehkamaliha: that is not a problem. just copy the JavaScript code in to your HTML file. but your serverside and HTML should be inside same site – Chamika Sandamal Oct 30 '11 at 12:13