简体   繁体   English

JSON响应AJAX中的格式错误

[英]Bad format in JSON response AJAX

I have the follow code, it's call a web service in other PC server. 我有以下代码,它被称为其他PC服务器中的Web服务。 Data parameters are ok. 数据参数还可以。

"{ ControlId: 'ZAsociated_26037', TaskId: 1495613, UserId: 166396,  TBodyId: ''}"
 $.ajax({
        type: "POST",
        cache: false,
        dataType: "json",        
        contentType: "application/json; charset=utf-8",
        data: "{ ControlId: '" + controlId + "', TaskId: " + taskid + ", UserId: " +
              userId + ",  TBodyId: '" + $(tbody).attr("id") + "'}",
        url: getWSPath() + "/GetZAsociatedResults", // CARGAR AQUI LA DIRECCION DEL WEBSERIVCE
        success: function (data) {         

            if (data != null && data.d != "[]") loadAsociatedTable(controlId, data.d);
           $("#loadingImg" + controlId).remove();
        },
        error: function (xhr, ajaxOptions, thrownError) {
           $("#loadingImg" + controlId).remove();
           alert("Error al obtener los datos: " +thrownError + "\nCodigo de error: " +xhr.status);          
        }
    });

The error is, that when i have the data.d result of WS, JSON add adicionals "\\" chars in string: 错误是,当我获得WS的data.d结果时,JSON在字符串中添加了adicionals“ \\”字符:

The begin of response: [{\\"Nombre del Documento\\":\\"Indemnizacion/Factura. 22076 - Proveedor - Sin: 38653 Global: No\\",\\"Estado\\":\\"Pago finalizado\\", 回复的开始: [{\\“ Nombre del Documento \\”:\\“赔偿/事实。22076-Proveedor-Sin:38653 Global:否\\”,\\“ Estado \\”:\\“ Pago finalizado \\”,

I try to replace "\\" to null string but it doesnt work 我尝试将“ \\”替换为空字符串,但不起作用

This AJAX when success call to loadAsociatedTable function and this do: 成功调用loadAsociatedTable函数时,此AJAX会执行以下操作:

 for (var i = 0; i < $.parseJSON(data).length;i++){

and have an error in $.parseJSON(data).length because don't converter this string to object. 并在$ .parseJSON(data).length中出错,因为请勿将此字符串转换为对象。

I checked in Chrome and the JSON is ok, without this bar "\\", and recognize that like a object, the problem is in IE v.11 only. 我在Chrome浏览器中进行了检查,并且JSON可以正常显示,没有带“ \\”的分隔符,并且发现它像一个对象一样,仅在IE v.11中存在问题。

The server's JSON response is invalid. 服务器的JSON响应无效。 Some server programmer has misunderstood JSON double quote escaping and tried to apply it everywhere. 一些服务器程序员误解了JSON双引号转义,并试图将其应用到任何地方。 Have the server return valid JSON. 让服务器返回有效的JSON。 Most server environments have standard JSON libraries which will construct JSON which is not invalid. 大多数服务器环境都有标准的JSON库,这些库将构造无效的JSON。 The correct JSON is of course just 正确的JSON当然就是

[{"Nombre del Documento":"Indemnizacion/Factura. 22076 - Proveedor - Sin: 38653 Global: No","Estado":"Pago finalizado", ...

You can try to fix the JSON yourself, by replacing \\" with " , which would just be 您可以尝试自己修复JSON,方法是将\\"替换为"

replace(/\\"/g, '"')

but the problem is that you will also destroy \\" sequences representing double quote marks inside string values. That's quite a tricky problem to solve. So the best solution is to get the server to send down correct JSON to start with. 但是问题是您还将破坏字符串值中表示双引号的\\"序列。这是一个棘手的问题。因此,最好的解决方案是让服务器开始发送正确的JSON。

Can you Post the complete JSON which will help me to analyze In meanwhile try this 您能否发布完整的JSON,这将有助于我同时进行分析

Using Eval on data.d like eval("{"+data.d+"}"); 在data.d上使用Eval就像eval(“ {” + data.d +“}”);一样;

or JSON.Parse(data.d) 或JSON.Parse(data.d)

try validate you json here http://jsonlint.com/ 尝试在这里验证您的json http://jsonlint.com/

Finally I could solve the problem, I used JSON.parse() instead $.parseJSON(), maybe is JQuery version. 最后我可以解决问题,我使用JSON.parse()代替$ .parseJSON(),也许是JQuery版本。 AJAX response contains this additionals "\\" but when I parse this object, JS convert it without problems. AJAX响应包含此附加字符“ \\”,但是当我解析此对象时,JS对其进行转换没有问题。 Thanks for your help! 谢谢你的帮助!

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

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