【问题标题】:Remove xml tag header from JSON response webService C#从 JSON 响应 webService C# 中删除 xml 标记标头
【发布时间】:2017-03-03 20:42:15
【问题描述】:

我的问题是我在 ASP.net 上有一个 web 服务,它返回一个 JSON 字符串对象(没有 XML)。但是当我调用 webmethod 时,我有这个:

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

在客户端,消息错误是这样的:Unexpected token &lt; 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 库对我不起作用,因为我在不使用它时得到了相同的结果。

【问题讨论】:

  • totalAcumulado序列化前的内容是什么?
  • 注意:这只是一个修复,而不是完美的解决方案。你不能打破你的字符串只返回它的 JSON 部分吗?假设您的 JSON 保持不变,您可以这样做 queHAY = queHAY.subString(queHay.indexOf("{"), queHAY.indexOf("}")+1);
  • 貌似和这篇文章差不多:stackoverflow.com/questions/11088294/…
  • 非常感谢大家...我在这里找到答案:stackoverflow.com/questions/11088294/…: this.Context.Response.ContentType = "application/json; charset=utf-8"; this.Context.Response.Write(serial.Serialize(city));

标签: c# json web-services


【解决方案1】:

感谢大家

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2011-01-04
    相关资源
    最近更新 更多