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.

4
  • what's the content of totalAcumulado before serialization? Commented Mar 3, 2017 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); Commented Mar 3, 2017 at 20:57
  • Seems similar to this article: stackoverflow.com/questions/11088294/… Commented Mar 3, 2017 at 21:05
  • Thanks a lot, everyone...I find my answer here: stackoverflow.com/questions/11088294/…: this.Context.Response.ContentType = "application/json; charset=utf-8"; this.Context.Response.Write(serial.Serialize(city)); Commented Mar 6, 2017 at 18:14

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.