How to call WebMethod?

I am trying to call WebMethodfrom JavaScript. So far I:

EMSWebService.asmx:

namespace EMSApplication.Web.WebServices
{
    /// <summary>
    /// Holds the Webservice methods of EMSApplication
    </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]     
    [System.Web.Script.Services.ScriptService]
    public class EMSWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

On the aspx page, I added the following:

<asp:ScriptManager ID="ScriptManager" runat="server">
    <Services>
        <asp:ServiceReference Path="~/WebServices/EMSWebService.asmx" />
    </Services>
</asp:ScriptManager>
<input onclick="callWebMethod();" id="btn" type="button" value="Click Me" />

And JavaScript:

<script type="text/javascript">
    function callWebMethod() {
        EMSApplication.Web.WebServices.EMSWebService.HelloWorld(OnComplete, OnError);            
    }

    function OnComplete(result) {
        alert(result);
    }

    function OnError(result) {
        alert(result.get_message());
    }

</script>

But the method does not execute. I get the following JavaScript error:

EMSApplication is undefined.

Is there anything I can't see? Do I need to do a different configuration?

The structure of the project is shown below:

enter image description here

JavaScript and components are located in Login.aspx.

Is there a url value [WebService(Namespace = "http://tempuri.org/")]


Edit:

I also tried this with jQuery and modified the aspx page as:

$(document).ready(function () {
        $("#btn").click(function () {                
            $.ajax({
                type: "POST",
                url: "../WebServices/EMSWebService.asmx/HelloWorld",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(response.d);                        
                },
                failure: function (msg) {                        
                    alert(msg.d);
                }
            });
            return true;
        });
    });

I wrote System.Diagnostics.Debug.WriteLine("Hello World");inside WebMethod, it does what it prints "Hello World" in the Visual Studio output window, but I do not get a warning message.

+5
source share
2 answers

, scripthandlerfactory, web.config... http://msdn.microsoft.com/en-us/library/bb398998.aspx

+1

.

WebMethod, SomePage.aspx:

[WebMethod]
public static String DoSomething(String shiftName)
{
    return shiftName+" hi there";
}

: -? HTTP, HTTP POST :

POST http://localhost:53638/SomePage.aspx/DoSomething HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: qps-ploc,en-US;q=0.5
Accept-Encoding: gzip, deflate
Host: localhost:53638
Connection: Keep-Alive
Content-Length: 23
Content-Type: application/json;charset=utf-8

{'shiftName':'contoso'}

:

  • HTTP: POST (GET )
  • aspx SomePage.aspx/[MethodName]. :

    SomePage.aspx/DoSomething

  • JSON. : shiftName. , JSON:

    {'shiftName':'contoso'}
    
  • JSON, Content-Type:

    ContentType: application/json;charset=utf-8
    

, WebMethod , hi there , -:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 24
Connection: Close

{"d":"contoso hi there"}

HTTP JSON, , d. , d, .

, WebMethod, http (, , COM, #, Java, Delphi).

, jQuery.

$.ajax({
       type: "POST",
       url: 'Catalogo.aspx/checaItem',
       data: "{ id : 'teste' }",
       contentType: 'application/json; charset=utf-8',
       success: function (data) {
           alert(data);
       }
});

. , . .

+3

All Articles