Closed Bug 1031985 Opened 10 years ago Closed 4 years ago

console reports syntax error for valid json fetched via jquery.ajax

Categories

(Core :: Networking, defect, P4)

x86
All
defect

Tracking

()

RESOLVED INVALID

People

(Reporter: shaver, Unassigned)

References

()

Details

Attachments

(3 files)

981 bytes, application/vnd.mozilla.xul+xml
Details
97 bytes, text/plain
Details
188.28 KB, image/png
Details
I have the following content a JSON file:

------
[
  {"author": "Pete Hunt", "text": "This is one comment"},
  {"author": "Jordan Walke", "text": "This is *another* comment, wow"}
]
------

When it's loaded via $ajax, I get an error in the console:

syntax error comments.json:1

The file seems to be parsed correctly nonetheless, and JSON.parse from the console correctly decodes it.

Chrome doesn't report an error here.

Nightly of 6-27 vintage.
This sounds like a problem with $ajax. Is that jquery? Try debugging the library, see what triggers the error.
This is not jquery, nor custom library-specific. I'm uploading an .xul and a json file to demonstrate the problem. The issue still exists in Firefox 33.
Attached file loadJsonFile.xul
Attached file aFile.txt
The error appears also when we open a json file using XMLHttpRequest
------
var req = new XMLHttpRequest();
req.open('GET', "file.json", false); 
req.send(null);
if((req.status == 200) || (req.status == 304) ) {
  return(JSON.parse(req.responseText));
------

My file "file.json" contains:
------
[
    {
        "firstName": "John",
        "lastName": "Doe"
    },
    {
        "firstName": "Anna",
        "lastName": "Smith"
    }
]
------

On the console log: syntaxe error file.json:1:1

Firefox 36.0.1 on Linux/manjaro
What is the value of the "Content-Type" header being returned from the server? If I had to take a guess, it isn't being set, and Firefox is likely defaulting the content type to "text/xml". Since the content type is json and not xml, the xml parser cannot parse it and it results in "Syntax error" being written to the console.

Try setting the content type on the server to "application/json" or "text/plain" and see if the syntax error still occurs.
confirm at version 41.0
the content-type from server header is 

Content-Type:"application/json;charset=UTF-8"

already. at chromium work fine.

just try, about few second.. why still after year...
(In reply to arliweng from comment #7)
> confirm at version 41.0
> the content-type from server header is 
> 
> Content-Type:"application/json;charset=UTF-8"
> 
> already. at chromium work fine.
> 
> just try, about few second.. why still after year...

that r not json string or prase problem, because work fine when i use that string for JSON.parse in the console, but sohw me error in the developer tools-network-response.
fixed @ version 41.0.1
(In reply to arliweng from comment #9)
> fixed @ version 41.0.1

update: still when unknown reason. but when i reopen developer tools, that problem gone.
Bug still exists in 46.0 and it's really very simple.

Content-Type: "application/json;charset=UTF-8"
- cause "syntax error" message at  json 1:1, but all work fine except message


Content-Type:"application/json;charset=UTF-8"
- all ok.

Yes, it's just one space
Whiteboard: [platform-rel-jQuery]
platform-rel: --- → ?
platform-rel: ? → ---
Whiteboard: [platform-rel-jQuery]
Product: Firefox → DevTools
(In reply to andrew.filonov from comment #11)
> Bug still exists in 46.0 and it's really very simple.
> 
> Content-Type: "application/json;charset=UTF-8"
> - cause "syntax error" message at  json 1:1, but all work fine except message
> 
> 
> Content-Type:"application/json;charset=UTF-8"
> - all ok.
> 
> Yes, it's just one space

I can't reproduce this. Is this still an issue for you ?
Flags: needinfo?(andrew.filonov)
Priority: -- → P4
I am loading a js file and it's complaining about an XML parse error also a 1:1....
this seems to be only happening in Firefox on local (file:///), ex.

```
$.ajax({
    url: 'file:///C:/Code/project/projectdata.js',
    method: 'GET',
    contentType: 'text/plain',
    dataType: 'text/plain',
    success: function (data) {
      console.log(data);
    }
  });
```

If I host it with xampp and run it as http://localhost there is no error...
I am using FF 61.0.1 x64, Win10.
joedf, which version of jquery are you using ? I wasn't able to reproduce in https://high-water.glitch.me/ ($.ajax fails for some reason, and I don't know jquery enough to know what's happening)
Flags: needinfo?(joedf)
Whoops, my bad.

$.fn.jquery reports "1.11.1"
Flags: needinfo?(joedf)
joedf, i can't reproduce the issue myself. 
Would you be able to put up a glitch example and share it here ?
I don't think this can be produced on a hosted environment. I have tested this on hosted w/ xampp and got zero issues.
This is a minor issue as far as I can tell, because it will issue a warning, but the data can still be processed. However, this warning can cause a break in a webapp when loading. This can be circumvented by using a try-catch statement.

The steps I would take to try to reproduce on a different computer:
1. make an external .js file with something like

var mydata = '<xml>PUT SOME EXAMPLE XML data here</xml>';

2. on local i.e. file:///c:/example/path/index.html try to load it with $.ajax with jQuery v1.11.1
3. see if the current js env has a var with mydata?
4. profit?

Not sure, hopefully this can help get to the bottom of this

Bug still present in Firefox 69.0.

We recently upgraded a few SAP BusinessObjects systems to 4.2 and part of the Central Management Console (CMC) won't render because of this issue.

To reproduce:

  1. Log into CMC
  2. Click on "Monitoring"
  3. Page never loads

In Firefox's console there are five "XML Parsing Error: syntax error" errors. Five files that have .properties extensions, are plain text, the server has sent without a content-type and are being loaded via JQuery (bundled version is 1.7.1).

Renders okay in every other browser I've tried.

Please let me know if there is any other information I can supply to help with this.

For what it's worth I'm also going to log a bug with SAP to ask them to fix the issue on their end (i.e. sending the .properties files without a Content-Type).

Just realised this is classified as a DevTools bug. The bug isn't in the DevTools, they are working as intended. The problem is in whatever component of Firefox (Gecko I'm guessing) that is taking a guess that a file with no Content-Type is "text/xml".

Component: Console → General
Product: DevTools → Firefox

Also, I'm seeing this issue on Windows 7. It isn't just a Firefox/Mac issue.

OS: macOS → All

Hi Nicolas,

The main problem is a

XML Parsing Error: syntax error
Location: https://wax-jujube.glitch.me/mytxt
Line Number 1, Column 1:

when a text file is sent from the server with Content-Type: text; charset=UTF-8 instead of text/plain; charset=UTF-8 (with plain). Firefox seems to assume that it is an XML file and tries to parse resulting in the Parsing error.

Here is a glitch where it can be reproduced: https://wax-jujube.glitch.me/

I've hit this bug now as well in a real world scenario where I do not have influence on what headers the third party server sends. FWIW, in Chrome there's no such XML parsing error.

I use current Nightly on Windows 10.

Probably not a DevTools bug, but something in Gecko/parser. Maybe you can forward to the right department.

Flags: needinfo?(andrew.filonov) → needinfo?(nchevobbe)
Has STR: --- → yes

moving to network

Component: General → Networking
Flags: needinfo?(nchevobbe)
Product: Firefox → Core

Bug 1502417 looks related, probably even duplicate, and also has STR. There a map on a website does not even load but does so in Chrome, so seems to cause big compat issues.

Also on stackoverflow quite a few people seem to be affected by this bug:

Might be a good idea if this bug could be re-triaged by the network group as it is already 6y old.

(In reply to Jens from comment #23)

Hi Nicolas,

The main problem is a

XML Parsing Error: syntax error
Location: https://wax-jujube.glitch.me/mytxt
Line Number 1, Column 1:

when a text file is sent from the server with Content-Type: text; charset=UTF-8 instead of text/plain; charset=UTF-8 (with plain). Firefox seems to assume that it is an XML file and tries to parse resulting in the Parsing error.

I think text is not a valid MIME type.
MDN says that a MIME type should always have both a type and a subtype. So, this might be an invalid bug.
What do you think?

Flags: needinfo?(nachtigall)
  1. Regardings web compat:
    Well, probably right. I do not know. But this causes web compat issues like seen in Bug 1502417 where a map on a page does not even load. So one could also argue that Chrome is wrong here and file a bug against Chromium. Not sure if they would do something. NI'ing to :miketaylr from web compat team (hope this is right, not sure who else). I guess Chromium falls back to "text/plain" when receiving "text" (not sure).
  2. Regarding error message:
    OTOH, I am not sure if the Firefox output of "XML Parsing Error: syntax error" is correct then and should not be Invalid/MIssing MimeType Subtype: Received Content-Type "text" without any Subtype or similar. The current error message is really misleading.
Flags: needinfo?(nachtigall) → needinfo?(miket)

Can't watch embedded video at https://animehub.ac/watch/boku-no-hero-academia-sub.15322?ep=160884&s=vservers host as Firefox console returns XML parsing error of the .m3u8. Any plans to address this ?

:uBlock-user When opening https://animehub.ac/watch/boku-no-hero-academia-sub.15322?ep=160884&s=vservers and DevTools Console I see no message about an XML parsing error. Could you maybe add steps to reproduce and a screenshot? So it's reproducable by whatever team will hopefully handle this. Thank you.

:kershaw Since :miketaylr from the web compat team is not responding and I am not a Mozillian I just NI back to you. Hope you can either forward to another Mozillian or fixed as part of the network team.

Personally, I think that though maybe not same as spec the way Chrome handles the parsing is more lax. Chrome seems to be "tolerant" here. Probably, the most realistic way to handle this is to mimic it.

Flags: needinfo?(sscarl24)
Flags: needinfo?(kershaw)
Attached image Auswahl_053.png

:uBlock-user Uh, I could reproduce now. One has to open the page first, and then open the DevTools' console. If the DevTools' console is already open, then the error does not show up. (guess cause the page has some spurious debugger; statements for obfuscation). See screenshot Auswahl__.png

Flags: needinfo?(sscarl24)

Yes, sorry I forgot to add that part of info. I found this to be a Firefox specific bug that started occuring to me, few days ago and as per https://stackoverflow.com/a/51000139 this is specific to Firefox only.

By "can't watch embedded video", I mean that the video keeps buffering endlessly and then fails, so never plays.

Sorry for the delay here -- it seems like this issue is really about Firefox defaulting to text/xml for resources without a declared Content-Type header, correct? (that's at least the case for the m3u8/HLS video issue described in Comment #28). It's not surprising to me that Firefox and Chrome do something different here. Anne, are you aware of any similar issues this should likely be duped against?

Flags: needinfo?(miket) → needinfo?(annevk)

(In reply to Mike Taylor [:miketaylr] from comment #33)

Sorry for the delay here -- it seems like this issue is really about Firefox defaulting to text/xml for resources without a declared Content-Type header, correct? (that's at least the case for the m3u8/HLS video issue described in Comment #28).

Yes, and also defaulting to text/xml for resources that declare Content-Type: text whereas Chrome defaults to text/plain for those as described in Comment 23.

(Besides this the console error message is misleading and could be improved)

text will fail to parse as a MIME type and per https://xhr.spec.whatwg.org/#response-mime-type it'll then be text/xml.

I filed https://bugs.chromium.org/p/chromium/issues/detail?id=1053973.

Status: NEW → RESOLVED
Closed: 4 years ago
Flags: needinfo?(kershaw)
Flags: needinfo?(annevk)
Resolution: --- → INVALID

What about the video failing to play ?

(In reply to uBlock-user from comment #36)

What about the video failing to play ?

The video fails to play because the server hasn't set the appropriate Content Type header (should be application/x-mpegURL or vnd.apple.mpegURL). You can try to contact the site and ask them to fix it.

If that's the case then how come the video played all this time ? This issue only started occurring since the past 10-15 days ago.

(In reply to uBlock-user from comment #38)

If that's the case then how come the video played all this time ? This issue only started occurring since the past 10-15 days ago.

:uBlock-user Many reasons:
a) Site has changed something.
b) Some other change in Firefox has caused it. Try if the error still persists in Nightly (scroll down in https://www.mozilla.org/en-US/firefox/channel/desktop/). If so you might want to use mozregression to find which change causes the bug (regression) and you can report it as a new bug. https://mozilla.github.io/mozregression/

(this post causes like mails to 50 people, so probably worth contacting some other support channel if you need further advice)

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: