简体   繁体   English

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

[英]Remove xml tag header from JSON response webService C#

My problem is that I have a webservice on ASP.net, which returns a JSON string object (no XML). 我的问题是我在ASP.net上有一个web服务,它返回一个JSON字符串对象(没有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 在客户端,消息错误是这样的: Unexpected token < in json at position 0

I expected this JSON: 我期待这个JSON:

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

a pure JSON 一个纯粹的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: 客户端(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>

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. 用于JSON的Newtonsoft库对我来说不起作用,因为当我不使用它时,我得到了相同的结果。

Thnaks a lot for everyone 给每个人带来很多好处

I find my answer here: asp.net asmx web service returning xml instead of json 我在这里找到了答案: asp.net asmx web服务返回xml而不是json

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM