How to return a pure Json (without XML encapsulation) using webservice in c#

Asked

Viewed 514 times

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:

Retorno encapsulado 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!!

2 answers

2

So that the attribute ScriptMethod be interpreted, you need to ensure that the module System.Web.Extensions be loaded.

Two amendments to the sitting system.web in his web.config are necessary:

<assemblies>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>

Original post in the OS in English.

1


Hudson, modify the method to void and return as follows:

 HttpContext.Current.Response.ContentType = "application/json";
 HttpContext.Current.Response.Write(js.Serialize(recibo));
 HttpContext.Current.Response.End();
  • 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, 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

Browser other questions tagged

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