0

I'm working on a project, where depending on user language, widget loads json file with content in that language. But since there are many languages, I need to check, if json file for that language exists, if it doesn't, need to load fallback json file (english).

My current code:

var cookieLang = Cookies.get('language');
widget = "../json/widget_";
ext = ".json";
en = 'en';
lv = 'lv';
ru = 'ru';

if (cookieLang === en){
    json_url = widget + en + ext;
}else if (cookieLang === lv){
    json_url = widget + lv + ext;
}else if (cookieLang === ru){
    json_url = widget + ru + ext;
}

$.getJSON(json_url, function(json){
...
});

With current code I get console error /json/widget_ru.json 404 (Not Found) when file doesn't exist.

What is the best way to check if json file exists and without console errors, if doesn't exist load default one?

4
  • ajax operations support several events Commented Nov 14, 2016 at 8:27
  • @Elvis check what is the url you are finally hitting to server to get the json file. 404 means the URL endpoint does not exist. Commented Nov 14, 2016 at 8:37
  • The url is ok, file doesn't exist on the server. What I need is to check if json file exists before $.getJSON function. If it returns error 404, json_url should be "../json/widget_en.json" Commented Nov 14, 2016 at 8:43
  • Instead of $.getJSON use $.ajax to handle your error event api.jquery.com/jQuery.ajax Commented Nov 14, 2016 at 8:44

2 Answers 2

2

If your app using only JS, the the below code will help you

var cookieLang = Cookies.get('language');
widget = "../json/widget_";
ext = ".json";
en = 'en';
lv = 'lv';
ru = 'ru';

json_url_default = widget + en + ext;

if (cookieLang === en){
  json_url = widget + en + ext;
}else if (cookieLang === lv){
  json_url = widget + lv + ext;
}else if (cookieLang === ru){
  json_url = widget + ru + ext;
}

$.ajax({
 url: json_url,
 type: "GET",
 statusCode: {
    404: function() {
      $.getJSON(json_url_default, function(json){
        //code here
     });
    }
 },
 success:function(json) {
    //code here
 }
});
Sign up to request clarification or add additional context in comments.

1 Comment

OK. I guess it kind of works now. Although I still get console error (404) file doesn't exist. Did it like this: code $.ajax({ url: json_url, type: "GET", statusCode: { 404: function() { json_url = widget + en + ext; fotoWidget(); } }, success:function(json) { fotoWidget(); } });
0

Implement file checking code on server side. check the below code ( if your app in PHP ).

Client side

var cookieLang = Cookies.get('language');

$.get('/json.php', {lang: cookieLang}, function(json){
    //code here
}.'json');

File : json.php

<?php

   $cookieLang = $_GET['lang'];

   $widget = "../json/widget_";
   $ext = ".json";

   $json_url_default = $widget . 'en' . $ext;

   $json_url = $widget . $cookieLang . $ext;

   if(file_exists($json_url))
     echo file_get_contents($json_url);
   else
     echo file_get_contents($json_url_default);

   exit;
?>

1 Comment

Unfortunately my app uses only JS. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.