从JavaScript客户端调用ASP.net Web服务

从JavaScript客户端调用ASP.net Web服务

问题描述:

我有一个previously编写客户端 - 服务器应用程序(由另一个程序员)。该客户端写的JavaScript。我需要编写与ASP.net一个新的Web服务,但我不知道如何调用Web方法。我sitation是从JavaScript 这里描述调用Web服务一样 但它并没有一个明确的答案。 (只是说使用WCF,我不知道该怎么做) (请注意,我的客户是来自服务器的完全不同的项目,假设它是写在Eclipse) 我想知道我可以调用默认情况下,在AA ASP.net Web服务创建的HelloWorld的方法,从简单的HTML code(包括JS)

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)

谢谢, 埃拉

请参考以下链接希望会为您提供如何使用JavaScript / Asp.net的Ajax或jQuery来调用Web服务的完整指南。

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

样品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();