How to consume an "asmx" Webservice via Jquery Ajax

Asked

Viewed 1,812 times

4

I created a button that calls the search method Ajax to consume a Web Service Asxm.

Because the Ajax does not consume the Web Service?

<asp:Button ID="btnPesquisar" runat="server" 
    Text="PesquisarEmpresa" OnClientClick="buscarEmpresa();"/>

The Ajax Jquery function

    function buscarEmpresa() {
        $.ajax({
            type: "POST",
            url: "../../webService/Ajax.asmx/GetEmpresa",
            data: "{'cnpj':'" + $("#inputCnpjEmpresa").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data.Cnpj);
            },
            error: function () {
                alert("Erro");
            }
        });

    }

My Asmx.Cs

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Ajax : System.Web.Services.WebService {

    public Ajax () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static Empresa GetEmpresa(string cnpj)
    {
        return EmpresaControler.RetornaEmpresa(cnpj); 
    }
}

1 answer

4


From all the research and the @Tiagosilva tips, I found that the annotation

[System.Web.Script.Services.ScriptService] 

It was commented, that’s why the script did not consume the webservice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.