Javascript 如何使用两个Ajax Get请求仅显示一条警报消息?
我使用两个Ajax Get请求,每个请求都有一个if条件,如果任何条件不适用,我必须保存原因并在一条警报消息中显示它。但我显示了两个警报,每个警报对应一个get请求。这是我的密码:Javascript 如何使用两个Ajax Get请求仅显示一条警报消息?,javascript,alert,display,get-request,Javascript,Alert,Display,Get Request,我使用两个Ajax Get请求,每个请求都有一个if条件,如果任何条件不适用,我必须保存原因并在一条警报消息中显示它。但我显示了两个警报,每个警报对应一个get请求。这是我的密码: var errorMessage = ""; $.get(url, function (responseGET) { var responseGETHtml = $(responseGET).find(".data-table tbody").html()
var errorMessage = "";
$.get(url, function (responseGET) {
var responseGETHtml = $(responseGET).find(".data-table tbody").html();
console.log("responseGETHtml", responseGETHtml, typeof responseGETHtml);
var rowCount = $(responseGETHtml).filter("tr").length;
if (rowCount > 1) {
errorMessage +=
"There is more than one result for invoice #" + invoiceId + "\n";
}
});
var url2 = "/einnahmen/" + customerId;
url = url2.trim();
$.get(url2, function (responseGET) {
var responseGETHtml2 = $(responseGET).find(".data-item-form form.form");
if (brutto < csvBrutto) {
errorMessage += "Brutto of csv is bigger than Brutto in table" + "\n";
}
if (errorMessage != "") {
alert(errorMessage);
} else {
responseGETHtml
.find(".form-choice .forminput-zahlungsweise")
.prop("checked", true)
.val("Überweisung");
$.post({
url: "/einnahmen/?filter[rechnung_nr]=" + invoiceId,
data: responseGETHtml.serialize(),
success: function (responsePOST) {
alert($(responsePOST).find(".message").text());
},
dataType: "html"
});
}
});
var errorMessage=”“;
$.get(url,函数(responseGET){
var responseGETHtml=$(responseGET.find(“.data table tbody”).html();
日志(“responseGETHtml”,responseGETHtml,responseGETHtml的类型);
var rowCount=$(responseGETHtml).filter(“tr”).length;
如果(行计数>1){
错误信息+=
发票#“+invoiceId+“\n”有多个结果;
}
});
var url2=“/einnamen/”+客户ID;
url=url2.trim();
$.get(url2,函数(responseGET){
var responseGETHtml2=$(responseGET.find(“.data项form.form”);
if(brutto
如何才能只显示一条包含所有错误的警报消息?我的想法是使用承诺,尤其是
errorMessage
作为参数来解决请求的结果const request1=新承诺(解析=>{
$.get(url,函数(responseGET){
var responseGETHtml=$(responseGET.find(“.data table tbody”).html();
日志(“responseGETHtml”,responseGETHtml,responseGETHtml的类型);
var rowCount=$(responseGETHtml).filter(“tr”).length;
const errorMessage=rowCount>1?`发票#${invoiceId}有多个结果:“”;
解决(错误消息);
});
});
var url2=“/einnamen/”+客户ID;
url=url2.trim();
const request2=新承诺(解析=>{
$.get(url2,函数(responseGET){
var responseGETHtml2=$(responseGET.find(“.data项form.form”);
const errorMessage=brutto{
//当两个请求都已解决时,执行此代码
//是否至少有一个请求已通过错误消息解决?
const hasError=errorMessages.some(error=>error!='');
if(hasrerror){
警报(errorMessages.join('\n'));
}否则{
//当两个请求都已解决且没有错误时,将执行此代码。
响应TML
.find(“.form choice.forminput zahlungsweise”)
.prop(“选中”,为真)
.val(“Überweisung”);
美元邮政({
url:“/eInNahman/?过滤器[rechnung_nr]=”+发票ID,
数据:responseGETHtml.serialize(),
成功:功能(responsePOST){
警报($(responsePOST.find(“.message”).text());
},
数据类型:“html”
});
}
});
bruh您有两次警报alert($(responsePOST.find(“.message”).text())代码>但第一个警报有一个条件ifwell yea条件得到执行
const request1 = new Promise(resolve => {
$.get(url, function(responseGET) {
var responseGETHtml = $(responseGET).find(".data-table tbody").html();
console.log("responseGETHtml", responseGETHtml, typeof responseGETHtml);
var rowCount = $(responseGETHtml).filter("tr").length;
const errorMessage = rowCount > 1 ? `There is more than one result for invoice #${invoiceId}` : '';
resolve(errorMessage);
});
});
var url2 = "/einnahmen/" + customerId;
url = url2.trim();
const request2 = new Promise(resolve => {
$.get(url2, function(responseGET) {
var responseGETHtml2 = $(responseGET).find(".data-item-form form.form");
const errorMessage = brutto < csvBrutto ? 'Brutto of csv is bigger than Brutto in table' : '';
resolve(errorMessage);
});
});
Promise.all([request1, request2]).then(errorMessages => {
// this code is executed when both request has resolved
// has at least one request resolved with an error message ?
const hasError = errorMessages.some(error => error != '');
if (hasError) {
alert(errorMessages.join('\n'));
} else {
// this code is executed when both request has resolved with no error.
responseGETHtml
.find(".form-choice .forminput-zahlungsweise")
.prop("checked", true)
.val("Überweisung");
$.post({
url: "/einnahmen/?filter[rechnung_nr]=" + invoiceId,
data: responseGETHtml.serialize(),
success: function(responsePOST) {
alert($(responsePOST).find(".message").text());
},
dataType: "html"
});
}
});