0

我的ajax函数:

function fetchData(varUrl, varFunc, varData) {
 if (varData == undefined) varData = "";
 var options = {
    type: "POST",
    url: varUrl,
    data: varData,
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    timeout: 8000,
    cache: false,
    beforeSend: ShowLoading(),
    success: varFunc,
    complete: HideLoading(),
    error: function (x, t, m) {
        if (t === "timeout") {
            alert("got timeout");
        } else {
            alert(t+"\r\n\r\n"+m);
        }
    }
  };
  //execute the ajax call and get a response
  var request = $.ajax(options);
}

  function funcA(data){
     $.each(data, function (i, item) {...}
  }

  function funcB(data){
     $.each(data, function (i, item) {...}
  }

C#代码:

    public ActionResult AjaxA()
    {
        try
        {
            using (var db = new MvcContext())
            {
                var history = from h in db.Histories.Where(h => h.Id == 2)
                return this.Json(history.ToList());
            }
        }
        catch(Exception)
        {
            return this.Json("");
        }
    }

     public ActionResult AjaxB()
    {
        var history = new List<MyDataType>();
        history.Add( new MyData(...));
          ...
        history.Add( new MyData(...));
        return this.Json(history);
    }

1)如果我使用 fetchData("AjaxA", funcA) 来获取数据,就可以了

2)如果我使用 fetchData("AjaxB", funcB) 来获取数据,它会失败并得到错误

JSON.parse:意外字符错误

但在这种情况下,firebug 也可以获得良好的 json 数据。

json 数据 [{"x":39,"y":115.5,"Data":16.743013706957576},{"x":39.5,"y":115.5,"Data":12.353‌​109643128887}]

4

1 回答 1

1
Parse error on line 10:
...      "Data": 12.353‌​109643128887    }
-----------------------^
Expecting '}', ',', ']'

给你,我已经在http://jsonlint.com/上验证了 JSON

当我在文本编辑器上复制 JSON 时,有几个字符被转换为?,请检查。它看起来像一个编码问题。

[{"x":39,"y":115.5,"Data":16.743013706957576},{"x":39.5,"y":115.5,"Data":12.353??109643128887}]
于 2012-12-11T11:37:44.547 回答