0

i have two different functions namely onLogin: function(){} and addvehicleDetails: function(){}

in onLogin function i am getting data through ajax return call which i am assigning a variable returnText and then that returnText i am using addvehicleDetails function.

but as per console i am getting undefined when i come to addvehicleDetails() function.

what i want to achieve is i want to access a same variable which is globally declare use by two different functions of which one is getting a value and another is using it.

//Global variable
var returnText = new Array();

Code is as below

//Login
        onLogin: function(){
            debugger;
            var that = this,
            username = that.get("username").trim(),
            password = that.get("password").trim();
            if (username === "" || password === "") {
                navigator.notification.alert("Both fields are required!",
                                             function () {
                                             }, "Login failed", 'OK');


                return;
            }

            var request = $.ajax({
                  url: "http://localhost:62373/api/Vehicle/Login",
                  type: "GET",
                  data: {strUsername: username, strPassword: password},
                  dataType: "text"
                });

                request.done(function(data, textStatus, jqXHR) {


                   alert("Success: " + data);
                    returnText[0] = data;
                    alert(returnText[0]);
                    debugger;
                    window.location.href = "iVehicle.html"


               });

        },


 //add vehicle details
        addvehicleDetails: function(){
            debugger;
            var that = this;
            vehicleName = that.get("vehicleName").trim(),
            vehicleType = that.get("vehicleType").trim(),
            registrationNumber = that.get("registrationNumber").trim(),
            fuelType = that.get("fuelType").trim(),
            fuelEfficiency = that.get("fuelEfficiency").trim(),
            insuranceBy = that.get("insuranceBy").trim(),


            dateCreated = new Date();
            var month = dateCreated.getMonth()+1;
            var day = dateCreated.getDate();
            var output = dateCreated.getFullYear() + '/' +
            (month<10 ? '0' : '') + month + '/' +
            (day<10 ? '0' : '') + day;
            myGuid =  returnText[0];
            //myGuid = document.getElementById("hdn").value;
            alert(myGuid);
            var request = $.ajax({
                  url: "myurl",
                  type: "GET",
                  data: {vname:vehicleName, vtype:vehicleType, regNumber:registrationNumber, ftype:fuelType, fefficiency:fuelEfficiency, insby:insuranceBy, datecreated:output, myid:myGuid},
                  dataType: "text"
                });

                request.done(function(data, textStatus, jqXHR) {
                    alert("Success: " + data + ' ' + ".");
                    that.clearForm();
               });


                 request.fail(function( jqXHR, textStatus, errorThrown ) {
                alert("Request failed:" + errorThrown + ' ' + textStatus);
                  // that.clearForm();

                });


                request.always(function(jqXHR, textStatus, errorThrown) {
                // alert("complete: " + errorThrown + ' ' + textStatus);
                      that.clearForm();
                 });

        },
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Trupti
  • 597
  • 4
  • 8
  • 17
  • You should probably have a look at http://stackoverflow.com/q/14220321/218196. While it's certainly possible to share the result of an Ajax call via a global variable, it's also harder to time/synchronize. If the second function tries to access the data before the response was received, then it won't work. – Felix Kling Dec 11 '13 at 06:01
  • please post your tried code so others can show you correct path. – Indranil.Bharambe Dec 11 '13 at 06:02
  • please refer above code Terror.Blade – Trupti Dec 11 '13 at 06:45
  • @FelixKling ... in this case 2nd function is not trying to access a data before response was received – Trupti Dec 11 '13 at 06:47
  • By doing `window.location.href = "iVehicle.html"`, you are **reloading** the page and thus a new JavaScript environment is created. – Felix Kling Dec 11 '13 at 06:56
  • @FelixKling but i want to redirect to another page after login so window.location.href = "iVehicle.html" is important – Trupti Dec 11 '13 at 07:00
  • Ok, but any variable you set will be lost. JavaScript variables don't persist across page loads. – Felix Kling Dec 11 '13 at 07:01
  • so in that function i omit window.location.href = "iVehicle.html"line is it possible to persist tha variable state – Trupti Dec 11 '13 at 07:08
  • You should save your array to something like localStorage before you redirect to another (or same) page. You can use JSON.stringify() and JSON.parse() as described [here](http://stackoverflow.com/questions/3357553/how-to-store-an-array-in-localstorage). – iForests Dec 11 '13 at 07:13

1 Answers1

0

try something like this

    function onLogin{
        // your ajax code goes here
        // after successfull completion of ajax call your  addvehicleDetails function
        var response = //response from ajax
        addvehicleDetails(response);
    }
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40