从JSON响应webService C#中删除xml标记头

时间:2017-03-03 20:42:15

标签: c# json web-services

我的问题是我在ASP.net上有一个web服务,它返回一个JSON字符串对象(没有XML)。但是,当我打电话给网络方法时,我有这个:

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

在客户端,消息错误如下:Unexpected token < in json at position 0

我期待这个JSON:

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

纯粹的JSON

这是我的网络服务代码:

[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;
    }
}

来自客户端(aspx页面)的代码是:

<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>

我在这个网站上搜索同样的问题但是没有用。用于JSON的Newtonsoft库对我来说不起作用,因为当我不使用时,我得到了相同的结果。

1 个答案:

答案 0 :(得分:0)

为每个人提供了很多帮助

我在这里找到答案: 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));