ajax返回值传给js全局变量
1.
$.ajaxSetup({ async : false //设置ajax为同步方式,异步方式的话在赋值时数据还未提取出来 });
var t = ""; var engineerMethod = $("#engineerMethod").val(); $.post("/civilizationconstruction/template!vali.action",{"templateUnit.engineerMethod":engineerMethod},function(data){ t = data;//赋值给全局变量 });
2.
var t = "";
$.ajax({ async:false, //使用同步请求,因为异步请求不能将返回值传给全局变量; type: "POST", url: "server.php", data: "", success: function(msg){
t=msg;
}
});