“不良形”在使用XMLHttpRequest加载JSON文件时,在Firefox中出现错误 [英] "not well-formed" error in Firefox when loading JSON file with XMLHttpRequest

查看:150
本文介绍了“不良形”在使用XMLHttpRequest加载JSON文件时,在Firefox中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的页面上的JavaScript加载一个包含JavaScript Object Notation格式的对象的文本文件时,我在Firefox 3.0.7的错误控制台中收到了一个格式不正确的错误。如果文件只包含JSON对象,则会产生错误。如果我将对象包装在< document>< / document>标记它不会产生错误。请求成功的方式,所以我可以忽略它,但我不希望我的错误日志填满这些消息。

这是一些示例代码来说明问题。首先,名为data.json的格式不正确的文件:

  {a:3} 

现在有一些代码加载文件:

  var req = new XMLHttpRequest(); 
req.open(GET,data.json);
req.send(null);

在Firefox错误控制台中产生以下错误:

格式不正确

file://path/to/data.json行:1

{a:3} <如果将data.json修改为:








$ b $ <文件> {a:3}< / document>

没有错误。我认为这是抱怨,因为普通的JSON文件不是一个格式良好的XML文档,所以我试图在发送调用之前覆盖MIME类型,强制它加载为纯文本,但是这并不起作用。 p>

  var req = new XMLHttpRequest(); 
req.open(GET,data.json);
req.overrideMimeType(text / plain);
req.send(null);
//仍然产生错误!

我将继续将我的JSON数据封装在XML文档中,以解决XMLHttpRequest正在执行,但我想知道是否有任何方法可以强制它只是加载纯文本不加批判,而不是尝试验证它。另外,除XMLHttpRequest之外,还有另外一种加载数据的方法可以用纯文本吗? 解决方案

您是否尝试过使用MIME为JSON类型?

  application / json 

你也可以配置你的服务器自动发送这个MIME类型的.json文件。


I'm getting a "not well-formed" error in the error console of Firefox 3.0.7 when the JavaScript on my page loads a text file containing an object in JavaScript Object Notation format. If the file contains nothing but the JSON object, it produces the error. If I wrap the object in <document></document> tags it does not produce the error. The request succeeds either way, so I could just ignore it, but I don't want my error log filling up with these messages.

Here is some example code to illustrate the problem. First, the "not well-formed" file called "data.json":

{ a: 3 }

Now some code to load the file:

var req = new XMLHttpRequest();
req.open("GET", "data.json");
req.send(null);

Which produces the following error in the Firefox error console:

not well-formed
file://path/to/data.json Line: 1
{ a: 3 }
- ^

If data.json is modified to this:

<document>{ a: 3 }</document>

There is no error. I assumed that it is complaining because the plain JSON file is not a well formed XML document, so I tried overriding the MIME type before the "send" call to force it to load as plain text, but that didn't work.

var req = new XMLHttpRequest();
req.open("GET", "data.json");
req.overrideMimeType("text/plain");
req.send(null);
// Still produces an error!

I am going to continue with wrapping my JSON data in an XML document to get around whatever validation the XMLHttpRequest is performing, but I'd like to know if there is any way I can force it to just load plain text uncritically and not try to validate it. Alternatively, is there another method of loading data besides XMLHttpRequest that can be used with plain text?

解决方案

Have you tried using the MIME type for JSON?

application/json

You could also configure your server to send this MIME type automatically for .json files.

这篇关于“不良形”在使用XMLHttpRequest加载JSON文件时,在Firefox中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆