我需要在success函数中声明一个var。但我得到了#34;未定义"。
$("#frm-registro").submit(function() {
x=existeCedula();
/*Here i get "undefined"*/
alert(x);
});
function existeCedula(){
var serializedForm = $("#frm-registro").serialize();
$.ajax({
type: "POST",
url: "<?php echo $this->webroot; ?>site/find_ced_ins",
async: true,
data: serializedForm,
success: function(data){
return data;
}
});
}
答案 0 :(得分:0)
Ajax调用是异步发生的,因此您的方法在调用完成之前返回。你需要做这样的事情:
$("#frm-registro").submit(function() {
x=existeCedula();
});
function existeCedula(){
var serializedForm = $("#frm-registro").serialize();
$.ajax({
type: "POST",
url: "<?php echo $this->webroot; ?>site/find_ced_ins",
async: true,
data: serializedForm,
success: function(data){
alert(data);
//Resume your logic here
}
});
}
你也可以做这样的事情,其中回调就是一个功能:
function existeCedula(callback){
var serializedForm = $("#frm-registro").serialize();
$.ajax({
type: "POST",
url: "<?php echo $this->webroot; ?>site/find_ced_ins",
async: true,
data: serializedForm,
success: function(data){
callback(data);
}
});
}