0

The script should take the user's session via ajax and transfer it to other ajax requests that take the data for the charts. The first request succeeds, but others have not, becouse sessionId for them is undefined. Tried to wrap them in a function, but unfortunately nothing happened. What can be done?

var sessionId,
    requestInWorkCount,
    requestInWorkPower;

function getSession(){
  $.getJSON("http://192.168.1.142/DashboardService.svc/web/jsonLoginUser?UserID=User1&UserPassword=123", {},
                    function(data) {
                    $.each(data, function (key, val) {
                      sessionId = val.toString();
                      return sessionId;
                    })
                });
};

function getData(session){
      $.getJSON("http://192.168.1.142/DashboardService.svc/web/jsonGetIndicator?SessionID="+session+"&IndNum=1", {},
                function(data) {
                $.each(data, function (key, val) {
                  requestInWorkCount = val;
                  return requestInWorkCount;
                })
            });
      $.getJSON("http://192.168.1.142/DashboardService.svc/web/jsonGetIndicator?SessionID="+session+"&IndNum=2", {},
                function(data) {
                $.each(data, function (key, val) {
                  requestInWorkCount = val;
                  return requestInWorkPower;
                })
              });
    };
$(document).ready(function(){
  getSession();
  getData(sessionId);
  setInterval('show()',1000); 

});
whois42
  • 176
  • 13

3 Answers3

3

ajax call is async so your second ajax call is executed before the first completes thats the reason sessionId is undefined.

Use the success function of first ajax call and call GetData() in it.

Also you don't need to return sessionId

function getSession(){
  $.getJSON("http://192.168.1.142/DashboardService.svc/web/jsonLoginUser?UserID=User1&UserPassword=123", {},
                    function(data) {
                    $.each(data, function (key, val) {
                      sessionId = val.toString();
                      getData(sessionId);

                    })
                });
};

you call like this now:

    $(document).ready(function(){

      getSession();


    });
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

The AJAX request are asynchronous and you cannot execute them sequentially.

An easy solution here would be to call getData() inside AJAX success block:

function getSession() {
    $.getJSON("http://192.168.1.142/DashboardService.svc/web/jsonLoginUser?UserID=User1&UserPassword=123", {}, function(data) {
        $.each(data, function (key, val) {
            sessionId = val.toString();
            getData(sessionId);
            return sessionId;
        });
    });
};
martynas
  • 12,120
  • 3
  • 55
  • 60
-1

You have to take the RESPONSE of the ajax request. You do not return it, I believe. But in your code you should perhaps do

sessionId = getSession();
getData(sessionId);

You forgot to dereference what it returned.

Jekk
  • 577
  • 8
  • 27