我有两个不同的功能,即onLogin: function(){}
和addvehicleDetails: function(){}
在onLogin
函数中,我通过ajax返回调用获取数据,我将分配变量returnText
,然后returnText
我正在使用addvehicleDetails
函数。
但是根据控制台,当我来addvehicleDetails()
功能时,我得到了不确定。
我想要实现的是我想访问一个相同的变量,该变量由两个不同的函数全局声明使用,其中一个获取值而另一个使用它。
//Global variable
var returnText = new Array();
代码如下
//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();
});
},
答案 0 :(得分:0)
尝试这样的事情
function onLogin{
// your ajax code goes here
// after successfull completion of ajax call your addvehicleDetails function
var response = //response from ajax
addvehicleDetails(response);
}