【问题标题】:Firefox JSON "not well formed" errror on valid JSON有效 JSON 上的 Firefox JSON“格式不正确”错误
【发布时间】:2011-04-28 13:10:16
【问题描述】:

我在 Firefox 中收到以下错误消息:

Error: not well-formed
Source File: http://school/courses.booking.add.php?1287657494723
Line: 1, Column: 1
Source Code:
{"type":"error","message":"You have already booked this course."}

如您所见,输出是有效的 JSON(它由 PHP 的 json_encode() 函数创建)。它与application/json MIME 类型一起提供。我认为错误可能与解析有关:

eval: function(json) {
    return eval('(' + json + ')');
}

但即使我不解析字符串,只是简单地提醒返回的 JSON 响应,错误仍然会出现。

相关问题:"not well-formed" error in Firefox when loading JSON file with XMLHttpRequest。他的解决方案是修复 MIME 类型。我的已经准确了,所以肯定是别的东西。

【问题讨论】:

  • 你有没有使用任何 FF4 + FireBug 的 beta 版本?我遇到了一些奇怪的问题......
  • FF3.6.11 + FireBug。不过,我为此使用了 Web 开发人员工具栏中的错误收集。我应该比现在更多地使用 FireBug。
  • 如果你不从 API 中提取数据,而是从变量 var myJSONObject = {"type":"error","message":"You have already booked this course."} 中读取数据,然后尝试 eval(myJSONObject) 是否仍然会出现同样的错误?
  • 如果我从一个对象中读取而不对其进行评估:var ret = {"type":"error","message":"You have already booked this course."};。如果我将它写为文本字符串并对其进行评估,它可以工作:var ret = '{"type":"error","message":"You have already booked this course."}'; ret = ajax.eval(ret);.
  • 它实际上在 Firefox 和我测试过的所有其他浏览器中都能完美运行。所以我可以放心地忽略错误消息。不过,很高兴知道是什么原因造成的。

标签: javascript json


【解决方案1】:

Web Developer Toolbar 中的 javascript 调试器似乎只是希望所有 Ajax 响应都是 XML,而不管 MIME 类型如何。其他任何内容都会产生“格式不正确”的错误。

【讨论】:

    【解决方案2】:

    我在使用 OpenJS 的 jxs 时遇到了同样的问题。在这种情况下,导致错误的原因是 load 属性中的条件(版本 3.01.A 中的第 33 行):

    //XML Format need this for some Mozilla Browsers
    if (http.overrideMimeType) http.overrideMimeType('text/xml');
    

    它让浏览器总是期待 XML。这可以很容易地解决这个问题:

    // XML Format needs this for some Mozilla Browsers
    if (format.charAt(0) === "x" && http.overrideMimeType) http.overrideMimeType("text/xml");
    

    既然现在比较format,代码也应该改变它的位置,应该放在后面

    format = format.toLowerCase();
    

    目前在第 38 行。因此,代码来自:

    32 //XML Format need this for some Mozilla Browsers
    33 if (http.overrideMimeType) http.overrideMimeType('text/xml');
    34
    35 if(!method) method = "GET";//Default method is GET
    36 if(!format) format = "text";//Default return type is 'text'
    37 if(!opt) opt = {};
    38 format = format.toLowerCase();
    39 method = method.toUpperCase();
    

    收件人:

    32 if(!method) method = "GET";//Default method is GET
    33 if(!format) format = "text";//Default return type is 'text'
    34 if(!opt) opt = {};
    35 format = format.toLowerCase();
    36 method = method.toUpperCase();
    37
    38 //XML Format need this for some Mozilla Browsers
    39 if (format.charAt(0) === "x" && http.overrideMimeType) http.overrideMimeType("text/xml");
    

    【讨论】:

      【解决方案3】:

      我在以前版本的 FireFox + FireBug 中遇到过这个问题,在 JSON 格式的内容之前/之后存在换行符。确保在服务器端输出 JSON 响应之前清除输出流。

      JSP 示例:

      out.clear(); out.println(json);
      

      【讨论】:

      • 不是这样的。根本没有空格。
      猜你喜欢
      • 2016-04-08
      • 2011-07-21
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2020-11-17
      相关资源
      最近更新 更多