I have created a button that calls the method findCompany which in turn is made a Ajax
request to consume a Web Service Asxm
.
Why does Ajax
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);
}
}