0

I have code snippet where I am pulling all the configs from back-end Node server through $.ajax. But when I hit My page first time, I do not get the response, where as When I check the log on node server, I do see the status for the request as 200 Ok. But I refresh the page twice or thrice I get the response properly.

Below is my code:

var appConfig = getConfig();
function setAppConfig(){
    var url = env.NEW_SERVER_URL + '/api/client/getConfig/carrier/';
    $.ajax({
        type: 'GET',
        contentType: "application/json",
        url: url,
        success: function (response) {
            //   console.log(response);
            var configData = JSON.stringify(response);
            sessionStorage.setItem('config', configData);
            return true;
        },
        error: function (error) {
            console.log("some error in connection with server");
            return false;
        }
    });
}

function getConfig(){
    var appConfig = "";
    // console.log(fetchFromSession('config'));
    if(fetchFromSession('config') !== null){
        appConfig = JSON.parse(fetchFromSession('config'));
    }else{
        if(setAppConfig()){
            appConfig = JSON.parse(fetchFromSession('config'));
        }else{
            console.log('Something went wrong in pullling the configs');
        }
    }
    return appConfig;
}

Node Server Code to respond the get request:

exports.config = ((req, res,next) => {
    let carrier = req.params.carrier;
    logger.info("getConfig started", {carrier: carrier});

     if(carrier){
          var msg = require(path.join('./../../../config/',carrier,'/config.json'));
          logger.info("Response for getScripts", {carrier: carrier, response: msg});
          res.send(msg);
     }
})

This script is loaded first on the page and all the dependent scripts are marked defer to wait for it to get loaded

Questions:

  • What is wrong with my code?
  • Why does it doesnt work first time but works on refreshing the page after that?
  • what do I need to do to get the successful response on first load itself?
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
  • There's no point in using `return true` and `return false` in the callback functions, nothing uses the return values. – Barmar Apr 19 '18 at 06:11
  • You set `contentType: 'application/json'` but you don't have a `data:` option with the JSON data. – Barmar Apr 19 '18 at 06:12
  • @Barmar it would be great if you can help me connect your above reported points as the reasoning to the problem I am facing.. because anyways my code works on second or third refresh – OM The Eternity Apr 19 '18 at 06:14
  • You don't seem to understand that AJAX is asynchronous. You're not waiting for the first AJAX call to complete before you call `fetchFromSession()`. – Barmar Apr 19 '18 at 06:15
  • That makes sense.. I was having that vibe.. but the thing is how can I control the complete script file loading to wait for ajax call to get completeted? as the fetchfromsession is beig called in the next dependent file. – OM The Eternity Apr 19 '18 at 06:17
  • you need to call `fetchFromSession()` in the `success:` function of AJAX, or use promises. – Barmar Apr 19 '18 at 06:18
  • See the duplicate question I linked to for lots of information on this. – Barmar Apr 19 '18 at 06:19
  • @Barmar i used the promise.. but the thing when My ajax request get resioved after the next scripts file loaded which already uses the parameters and hence it throws me an error – OM The Eternity Apr 19 '18 at 07:04
  • @Barmar as per provided link, I have used Promises, but I got stuck to other issue, Please navigate here: https://stackoverflow.com/q/49917088/184814 – OM The Eternity Apr 19 '18 at 09:31

1 Answers1

0

please try to use this using POST method

LIke

$.ajax({ type: "POST", async: true, cache: false,

Lavkush Gupta
  • 103
  • 13