0

I'm adding i18n support to an old javascript component. It didn't need xhr/fetch before, but it does still need to work in IE11.

I wan't to write:

    fetch("/libs/cq/i8n/dict." + lang + ".json")
        .then(function(response) { return response.json(); })
        .then(function(data) {
            dictionary = data;
        });

But I know IE doesn't support fetch. We've got jQuery available, so I tried:

    jQuery.ajax({
        url: "/libs/cq/i18n/dict." + lang + ".json",
        success: function (response) {
            dictionary = JSON.parse(response);
            onReady();
        }
    })

Now I'm getting this:

SCRIPT1014: Invalid Character

index.js (49,17)

I realize the 49,17 is referring to a position in the file, line 49, char 17. That's the d in dictionary = JSON.parse(response);

I've tested the response, it's strict valid JSON.

For some reason dictionary = eval(response) is working.

Joe Flynn
  • 6,908
  • 6
  • 31
  • 44

1 Answers1

0

The problem was that jQuery was already detecting that it was JSON and parsed it for me.

The Invalid Character error message was telling me that the string I passed to JSON.parse was an object.

All I needed on line 49 was:

            dictionary = response;
Joe Flynn
  • 6,908
  • 6
  • 31
  • 44
  • Odd this was the problem as it's not valid in any browser to call `JSON.parse()` on an object; you'll get a different error. – Rory McCrossan Jan 14 '20 at 16:58
  • Thanks for posting the solution to this issue. I suggest you try to mark your own answer as an accepted answer to this question after 48 hrs when it is available to mark. It can help other community members in the future in similar kinds of issues. Thanks for your understanding. – Deepak-MSFT Jan 15 '20 at 02:15