[英]Ajax Get Validation from function, then pass the response to another ajax?
我有一个ajax函数来验证页面上的文本框,然后需要使用该值做一些事情(在数据库中搜索并在文本框上显示结果)。 然后,我需要另一个不同的验证功能。 所以..
function validarRut(textBox) {
var resultado;
$.ajax({
type: "POST",
url: "AgendarInduccion.aspx/validarRut",
data: "{rut:" + JSON.stringify(textBox) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response){
alert("valido");
},
failure: function (response) {
alert("ERROR");
}
});
}
这是验证函数(有效!):以及要在数据库中搜索的函数:
function buscarEmpresa(textBox) {
var res;
res = validarRut(textBox.value); // I need the result, to works with this function.!
alert("VALOR" + res);
if (res) {
var resultado;
$.ajax({
type: "POST",
url: "/AgendarInduccion.aspx/buscarEmpresa",
data: "{RutEmpresa:" + JSON.stringify(textBox.value) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (response) {
resultado = response.d;
var nombre = resultado[0];
var razonSocial = resultado[1];
var direccion = resultado[2];
if (nombre == "nulo") {
alert("El Rut de la Empresa No se ha Encontrado en Nuestra Base de Datos, favor de ingresar!");
// crear empresa!
// la institucion no existe; se debe crear una!
//btGuardar.Visible = true;
//btCancelar.Visible = true;
//txtRutEmpresa.Enabled = false;
//txtNombreEmpresa.Enabled = true;
//txtRazonSocial.Enabled = true;
//txtDireccion.Enabled = true;
document.getElementById('<%= txtRutEmpresa.ClientID %>').disabled = true;
document.getElementById('<%= txtNombreEmpresa.ClientID %>').disabled = false;
document.getElementById('<%= txtRazonSocial.ClientID %>').disabled = false;
document.getElementById('<%= txtDireccion.ClientID %>').disabled = false;
$('#<%= txtRazonSocial.ClientID%>').val(razonSocial); //razon social desde SII
document.getElementById('<%=btGuardar.ClientID %>').style.display = 'inline';
document.getElementById('<%=btCancelar.ClientID %>').style.display = 'inline';
document.getElementById('<%=btActualizar.ClientID %>').style.display = 'none';
} else {
$('#<%= txtNombreEmpresa.ClientID%>').val(nombre);
$('#<%= txtRazonSocial.ClientID%>').val(razonSocial);
$('#<%= txtDireccion.ClientID%>').val(direccion);
}
},
failure: function () {
alert('No Realizado.');
}
});
}
}
我有第三个函数,它使用“ function validatearRut(textBox)”函数; 所以我不想成功放置搜索功能,因为我需要重新使用该功能。 有帮助吗? PD:异步:false对我来说不是一种选择。
这是我的解决方案!,非常感谢
function buscarEmpresaPromise(textBox) {
var res;
validarRutPromise(textBox.value).then(function (res) {
if (res.d) {
alert(">hola");
alert("Rut validado CORRECTAMENTE");
} else {
alert("Rut invalido, vuelva a ingresar porfavor: " + textBox.value);
}
})
}
另一个验证True或False的代码:
function validarRutPromise(textBox) {
var promise = new Promise(function (resolve, reject) {
$.ajax({
type: "Post",
url: "AgendarInduccion.aspx/validarRut",
data: "{rut:" + JSON.stringify(textBox) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
complete: function (response) {
resolve(response);
},
fail: function (response) {
reject(response);
}
});
})
return promise;
}
如果我正确地处理了您的问题,则Promises将解决您的问题。
基本上,您的方法必须返回一个Promise对象。 有了它们,您可以调用then(),一旦Promise被解决(成功)或被拒绝(错误),该方法就会立即执行。
所以基本上您的代码必须像这样工作:
function validarRut(value) {
return new Promise(resolve, reject){
$.ajax({
type: "get",
url: "/",
success: function (response) {
resolve(response); // when this is called, every then() function gets executed
},
failure: function (response) {
reject(response); // when this is called, every catch() function gets executed
}
});
}
}
function buscarEmpresa(textBox) {
var res;
validarRut(textBox.value).then(function (res) {
// res is whatever you passed the resolve function
if (res) {
textbox.text(res.toString());
// do whatever you want with the result
}
})
.catch(error){
// error is whatever you passed the reject function
}
}
$(document).ready(() => {
buscarEmpresa($('#result'));
})
观看实际操作: https : //jsfiddle.net/yygggbsa/
承诺是进行此类异步调用的方法。 但是请记住,它们在IE11中不可用:
https://caniuse.com/#feat=promises
因此,如果您需要对IE11等旧版浏览器的支持,则需要添加一个外部库。
进一步阅读:
在IE11中使用Promises: 如何在IE11 中实现承诺
MDN上的承诺: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.