本文介绍了json解析不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过ajax调用从服务器获取此响应

I am getting this response from my server with ajax call

var data =
[{
     "Response": {
        "ResponseStatus": {
            "Code": "1",
            "Description": "Success"
        },
        "TransactionReference": {}
    }
}, {
    "Response": {
        "ResponseStatus": {
            "Code": "1",
            "Description": "Success"
        },
        "TransactionReference": {}
    }
}];

Ajax调用:

$.ajax({
              "type":"POST",
              "url":"'.CHtml::normalizeUrl(array("packaging/calltag")).'",
              "data":$("#returnrequestcreationform").serialize(),
              "success":function(data){
                  $.each(data, function (key, val) {
                      console.log(key + val);
                  });
              },
 });

但是在日志中,它给了我

But in the log, it gives me this

0[
1{
2"
3R
4e
5s
6p
7o
8n

在尝试读取此对象时,我尝试了JSON.parse()和jQuery.parseJSON(),这些错误给我输入意外结束"错误.看起来像是正确的json返回给我了吗?知道为什么它像字符串一样循环遍历吗?

I have tried JSON.parse() and jQuery.parseJSON() which give me "unexpected end of input" error when trying to read this object. It looks like a proper json return to me? Any idea why it is looping through it as if it is a string?

推荐答案

您需要告诉函数数据类型是什么

You need to tell the function what the data type is

$.ajax({
          "type":"POST",
          "url":"'.CHtml::normalizeUrl(array("packaging/calltag")).'",
          "data":$("#returnrequestcreationform").serialize(),
           "dataType": "json",
          "success":function(data){
              $.each(data, function (key, val) {
                  console.log(key + val);
              });
          },
 });

这篇关于json解析不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:17