1

My problem is that I have a webservice on ASP.net, which returns a JSON string object (no XML). But when I call the webmethod I have this:

<string xmlns="https://www.puzzlebooks.com.ar">
{"Tipo":null,"Anio":0,"Mes":null,"CuentaContable":null,"Total":0,"OId":0}
</string>

On client side, the message error is this: Unexpected token < in json at position 0

I expected this JSON:

{"Tipo":null,"Anio":0,"Mes":null,"CuentaContable":null,"Total": 0,"OId": 0}

a pure JSON

This is my web service code:

[WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json) ]
public string AcumuladoTotalCompraVenta_MesActual()
{
    try
    {
        ModeloDeClases.Dominio.EstadisticaCompraVenta totalAcumulado = null;
        totalAcumulado = RepositorioServicios.RepositoryServices.getInstance().getEstadisticasServices().CompraVenta_TotalAcumuladoDelMesActual(23);

        if (totalAcumulado == null)
            totalAcumulado = new ModeloDeClases.Dominio.EstadisticaCompraVenta();

        string queHAY = new JavaScriptSerializer().Serialize(totalAcumulado);
        //  return totalAcumulado;   
        return queHAY;
    }
    catch (Exception ex)
    {
        this.Error(ex.Message);
        return null;
    }
}

And the code from client side (aspx page), is this:

<script type="text/javascript">
$(function () 
{  
    $.ajax
    ({  
        type: "POST",  
        dataType: "json",  
        contentType: "application/json",  
        url: "../webServices/wsEstadisticas.asmx/AcumuladoTotalCompraVenta_MesActual",
        data: "{}",  
        success: chartAcumuladoComprasVentas_MesActual,  

        error: function (request, status, error) 
        {
            alert('NOP!');
            alert(request.statusText);
            alert(error);
            alert(request.toString());
        }
    });  
})

function chartAcumuladoComprasVentas_MesActual()
{
    alert('IT S WORK!');
}
</script>

I search on this site about the same problems, but doesn't work. The library Newtonsoft for JSON don't work for me, because I get the same result when I didn't use it.

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • what's the content of totalAcumulado before serialization? – G.Anıl Yalçın Mar 03 '17 at 20:49
  • Note: This is only a fix but not the perfect solution. Can't you break your string to return only JSON part of it? Assuming your JSON remains same, you could do `queHAY = queHAY.subString(queHay.indexOf("{"), queHAY.indexOf("}")+1);` – Coder Mar 03 '17 at 20:57
  • Seems similar to this article: http://stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json – Corith Malin Mar 03 '17 at 21:05
  • Thanks a lot, everyone...I find my answer here: http://stackoverflow.com/questions/11088294/asp-net-asmx-web-service-returning-xml-instead-of-json: this.Context.Response.ContentType = "application/json; charset=utf-8"; this.Context.Response.Write(serial.Serialize(city)); – Cristian Remon Mar 06 '17 at 18:14

1 Answers1

1

Thnaks a lot for everyone

I find my answer here: asp.net asmx web service returning xml instead of json

this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(serial.Serialize(city));
Community
  • 1
  • 1