1

I have a Json(config/server.json) config file which has server configuration. eg:

{
    "server": "127.0.0.1"
}

I want to read that 'server' parameter and assign that value to my other jquery functions.

eg:

  var server = "";

$(document).ready(function() {
          loadConfigFile();
       loadCustomers();
});

  function loadConfigFile() {
          $.getJSON('config/server.json', function(jd) {
              $.each(data.server, function(i,s){
                  alert(s);
              });
          });
      };

  function loadCustomers() {
          $
                  .ajax({
                      type : 'GET',
                      url : server+':8080/cache/getCustomers',
      ...
}

My loadConfig file is not reading the JSOn file..What im doing wrong here?

Ratha
  • 9,434
  • 17
  • 85
  • 163
  • create a `var host = {"server": "127.0.0.1"};` and call : `host.server` to get the value. And your error is `function(jd)` and is the right is `function(data)` –  Oct 19 '17 at 23:23
  • @headmax can you profide code sample? var host = {"server": "127.0.0.1"} is not a valid json – Ratha Oct 19 '17 at 23:26
  • Ok give me 5min.. –  Oct 19 '17 at 23:27
  • First, this is duplicate question. Second, you should access config file like jd.server. Third, ajax is async function. You have to sync loadConfigFile with loadCustomers by promise or callback "success" – bigless Oct 19 '17 at 23:31
  • @bigless any code sample would be helpful? Im really new to javascript – Ratha Oct 19 '17 at 23:39

2 Answers2

1

Make sure that /config/server.json is exposed by server and valid address. You can try it by adding to browser. It should show entire file content. Next step, check browser console for errors..

Check this

$(document).ready(function() {
  $.getJSON('config/server.json', loadCustomers);
});

function loadCustomers(configFile) {
  $.ajax({
    type : 'GET',
    url : configFile.server+':8080/cache/getCustomers',
 });
}
bigless
  • 2,849
  • 19
  • 31
  • Sorry this is not working, I put alert inside loadCustomers(configFile) function, before ajax call, but not printing – Ratha Oct 19 '17 at 23:59
  • @Ratha this code does not print anything, but solve issues in your code. you can add alert(configFile.server) before $.ajax if you want to print server, but make sure that config file is accessible.. – bigless Oct 20 '17 at 00:08
  • I feel like my html couldn't read that json file in my local folder. Should I host that json file in a webserver and access it? What is the normal approach is in this scenario?My html and configuration files are in seperate folders called 'html,'config' etc... My requirement is, allowing users to access the html page from different hosts – Ratha Oct 20 '17 at 00:15
  • @Ratha then you found your issue. getJson is http opperation. It does not access the file directly. It access file through http protocol. Another approach is require.js... – bigless Oct 20 '17 at 00:18
  • So for getJson, if i register the config folder location to be accessed through an http link, (eg: http://myhost:8080/config) how should i read that from getJson method? – Ratha Oct 20 '17 at 00:20
  • But one more quick question, I have another javascript function which alredy takes one parameter call 'id) ( eg:function resetClient(id) { .ajax({ url: ..) To pass 'configFile ' as argument what should I do? – Ratha Oct 20 '17 at 00:25
  • I sorted out with a variable..Thanks ..It is all working – Ratha Oct 20 '17 at 00:30
  • Glad to hear it – bigless Oct 20 '17 at 00:31
0

In your config/server.json add this :

{"server": "127.0.0.1"}

You client script :

var server = ""; // don't need

$(document).ready(function() {
          loadConfigFile();
       loadCustomers();
});

  function loadConfigFile() {
          $.getJSON('config/server.json', function(data) {
              //HERE YOu need to get the name server
              alert(data.server);
          });
      };

  function loadCustomers() {
          $.ajax({
                      type : 'GET',
                      url : server+':8080/cache/getCustomers',
                       success:function(data){
                          alert(data);
                          console.log(data);
                        $.each(data, function(i,s){  
                            alert(s);
                         });
                      }
}
  • @Ratha Post your json i think you got a problem let me showed server.json if isn't to biggest i can adapt from this the script. –  Oct 19 '17 at 23:46
  • this only prints server, but does not solve async operations. server in loadCustomers will be empty string.. – bigless Oct 19 '17 at 23:55
  • but still is is not printing alert box – Ratha Oct 19 '17 at 23:57
  • i can show server.js there are only there ? `{"server": "127.0.0.1"}` or other datas? because if you got only this data you can loop there are any other data. if you want to take you data form loadCustomers you need to add an alert there. –  Oct 20 '17 at 00:00
  • Only this line.It si valid json.{ "server": "127.0.0.1" } – Ratha Oct 20 '17 at 00:01
  • So is the reason why you got nothing isn't an array is only an simple object you can't loop from nothing. –  Oct 20 '17 at 00:02
  • I feel like my html couldn't read that json file in my local folder. Should I host that json file in a webserver and access it? What is the normal approach is in this scenario? My html and configuration files are in seperate folders called 'html,'config' etc..My requirement is, allowing users to access the html page from different hosts – Ratha – Ratha Oct 20 '17 at 00:15
  • to put the json inside a folder javascript/json/ i guess is simple to access them very quickly. –  Oct 20 '17 at 00:17
  • You can html/javascript/json/ but the config i dunno exactly how you will server peoples and allowed or not, may be i need to info about the situation, context. –  Oct 20 '17 at 00:24
  • Thanks a lot...Issue was I didnt expose config folder in my server..SO html could not read it.. – Ratha Oct 20 '17 at 00:26
  • @Ratha ok i see, but in general we add some session and token but in your case i think is for lan web server. and you don't exposed of the web because people can understand how to but you can use obfucator of JS to add a little plus for people who are curious... –  Oct 20 '17 at 00:32