36

I used i18n plugin for load *.properties file for translation and its working fine on android platform but same library not working on IOS 10.3.1. It gives me below error:

enter image description here

i have done some changes in i18n library but still its not working.

function loadAndParseFile(filename, settings) {
    $.ajax({
        url: filename,
        async: false,
        cache: settings.cache,
        crossDomain: true,
        jsonpCallback: 'callback',
        contentType: 'text/plain;charset=' + settings.encoding,
        dataType: 'text',
        success: function (data, status) {
            parseData(data, settings.mode);
        }
    });
}

In above code:

i have been added Cross-Domain 'true' and datatype 'text'.. when i changed datatype 'text' to 'jsonp' its working but it gives .properties file error. Please check below error..

enter image description here

That means. file is loaded, but inner data format is different.

ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
  • Yes, the log tells you that the `Messages.properties` file is invalid ("Unexpected identifier 'User'). Is this a static file or generated? Either way: You need to fix it. – Wukerplank Jun 23 '17 at 09:39
  • @Wukerplank Yes, Its Static file.. –  Jun 23 '17 at 09:44
  • Are you passing a valid URL here... `$.ajax({ url: filename, ... })`? –  Jun 27 '17 at 13:01
  • @joshuamabina.. Yes.. URL is Valid.. its file path –  Jun 28 '17 at 07:22
  • @Goku you should put that file on your server and then try to access it from the server. IOS has different file structure than windows. That is why it is working on windows and not on IOS and AJAX call needs a protocol work – Shubham Jul 03 '17 at 06:47
  • @all Please give me solution.. i tried above but it didn't work.. –  Aug 04 '17 at 08:59
  • @Goku as @Shubham said, try uploading those files into a server, the files are loading with `file://` protocol that may work in windows, but IOS is more strict and maybe its blocking those calls – Frankusky Nov 05 '17 at 02:22
  • You add header on ajax call – JD Savaj Dec 09 '19 at 10:34
  • headers: { 'Access-Control-Allow-Origin':'Your Site Host' }," – JD Savaj Dec 09 '19 at 10:40
  • JSONP works differently, you have to wrap it with callback function and resolve it to get right resource you need. This is just temp. Always use CORS headers enabled from server side. – pavanjoshi May 21 '21 at 12:47
  • keep the datatype text and try consoling error error: function(xhr, status, error) { console.error("Error loading file: " + error); console.error("Status: " + status); console.error(xhr); } – Hammad Ahmed khan Aug 28 '23 at 07:07

2 Answers2

0

If you are using now JSONP instead of text, the file will be loaded as javascript code, so if contents are not valid javascript code it will fail.

Surround data with a global variable assignation or a function call:

    window.variable = "_DATA_"; // or
    functionName("_DATA_");

If _DATA_ are JSON format, then you don't need surround with quotes, otherwise you'll need to use "_DATA_" because without quotes it will not be valid javascript syntax.

user1039663
  • 1,230
  • 1
  • 9
  • 15
0

add this line to your ajax parameters

xhrFields: {
    withCredentials: true
}