3
I’m with a webservice running locally, which conducts queries directly in a database through a string type parameter. Follow the outcome of the consultation:
The second moment I have an application in javascript (Jquery) that consumes the webservice via Ajax. After some research I came to the conclusion that the problem is the XML encapsulation, because I’m trying to consume the Json via Ajax.
Jquery application trying to consume wbeservice c# via Ajax:
$("#btnConsultar").on("click",function(){
var NTalao = $("#campoTalao").val();
$.ajax({
url: "...",
type: "GET",
contentType: "application/json",
data: {"Talao": NTalao},
success: function(data){
$("#resDIV").html(data);
},
error: function(){
alert("Erro!");
}
});
});
Webservice c# returning encapsulated Json:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string Consultar(string _Talao)
{
Model1 cadastro = new Model1();
clsRecibo recibo = new clsRecibo();
var consulta = from a in cadastro.TB_Recibo
where a.Talao == _Talao
select a;
foreach (var linha in consulta)
{
recibo.Talao = linha.Talao;
recibo.Apresentante = linha.Apresentante;
recibo.TipoServico = linha.TipoServico;
recibo.Status = linha.Status;
recibo.Obs = linha.Obs;
}
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(recibo);
}
Basic question: Is it possible to remove this XML package from the query?? Otherwise, I can perform the query by the application otherwise?
Thank you!!
I made these changes and the webservice returned a pure Json, but I still can’t consume it via Ajax, in the app. Can it be the url? I’m consulting locally... tested urls:
http://localhost:50318/ServiceCRI.asmx/Consultar
,– Hudson
Hudson, see if it can refer to your ajax call, I happened error in the header of the http request: Access-Control-Allow-Headers, I solved this by removing the content-type of the ajax call in the client side Anyway, see if an error appears in your local test on the console
– Rodrigo