Java ajax,如果是,则显示模态,如果不是,则继续使用另一个ajax循环
我花了一天的时间想知道如何解决这个问题,我真的试图寻找答案,但我想我不能或者我不明白 以下是解决我的问题的步骤: 1.-我会做一个for,所以我必须使用ajax逐个处理每条记录(我看到使用async:false可以让我一个接一个地处理,而不是同时处理所有记录,我的意思是它就像一个fifo,对吗?因为我需要这样做,因为我使用的是httpsession,一些变量需要一个接一个地更改,这就是为什么我试图一个接一个地处理每个记录,尽管在我的示例中我没有显示这一点) 2.-如果我的验证为1,我将调用下一个ajax,但如果是2号,我必须要求用户放置一些东西,然后继续使用另一个ajax,然后这个ajax将调用另一个ajax作为1号(这是我的一个问题,因为当我显示模式时,模式会显示,但它会在循环中继续,所以我需要停止ajax,直到用户放置一些东西,当他单击“保存更改”时,它必须继续循环,尽管我说过这不会发生(我尝试使用了一段时间(true)但我没有机会让我的浏览器停止脚本),我的程序显示模式并继续循环(无论发生什么) 3.-如果用户看到模式并单击close,我不会继续使用ajax,但我会继续使用循环或for,因为我需要处理所有事情 这是模态的一个示例: 现在,正如你所看到的,是的,我的模态出现了,但我的循环无论发生什么都会继续:( 正如我所说,我需要这个: 1.-为了处理我所有的东西,我在做一个for,但根据答案,有时我必须显示模式,因此当用户单击savechanges时,它将调用另一个ajax,然后调用另一个ajax并继续循环,但是如果用户单击close well,我必须停止下一个ajax调用,并在下一个位置继续执行我的for一次又一次,直到我停下来 2.-换句话说,如果我在ajax 2中的结果是1,我必须调用我的ajax编号1,2,4,但是如果我在ajax编号2中的结果是==2,那么我必须显示模式并停止for,因为它继续循环,根据answe,如果是save changes,那么我必须调用ajax3,这将调用ajax4,但是如果它关闭,那么我必须继续在下一个循环中,这可能再次发生,取决于答案 以下是我的示例代码: JSP: Servlet 2Java ajax,如果是,则显示模态,如果不是,则继续使用另一个ajax循环,java,jquery,ajax,spring-mvc,Java,Jquery,Ajax,Spring Mvc,我花了一天的时间想知道如何解决这个问题,我真的试图寻找答案,但我想我不能或者我不明白 以下是解决我的问题的步骤: 1.-我会做一个for,所以我必须使用ajax逐个处理每条记录(我看到使用async:false可以让我一个接一个地处理,而不是同时处理所有记录,我的意思是它就像一个fifo,对吗?因为我需要这样做,因为我使用的是httpsession,一些变量需要一个接一个地更改,这就是为什么我试图一个接一个地处理每个记录,尽管在我的示例中我没有显示这一点) 2.-如果我的验证为1,我将调用下一个
package com.dot.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServeletOne
*/
@WebServlet(name = "two", urlPatterns = { "/two" })
public class ServeletOne2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServeletOne2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Yep i did a post");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int tmp = (int) ( Math.random() * 2 + 1);
System.out.println(tmp);
out.println(tmp);
}
}
Servlet三
package com.dot.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServeletOne
*/
@WebServlet(name = "three", urlPatterns = { "/three" })
public class ServeletOne3 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServeletOne3() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Yep i did a post");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("three");
}
}
servlet四
package com.dot.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServeletOne
*/
@WebServlet(name = "four", urlPatterns = { "/four" })
public class ServeletOne4 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServeletOne4() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Yep i did a post");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("four");
}
}
为了解决我的错误,我使用了一些标志
var positionNumber = 0;
var flagPressedButton = false;
在我看来,我用了另一种方式
for(i; i < 10;){
ajaxOne();
i++;
if(continuar == false) break;
(i;i<10;)的{
ajaxOne();
i++;
如果(continuar==false)中断;
当我的模态出现时,如果一切正常,我会再次模拟一次点击
$(“.clickme”).click()
这是我所有的js代码
<script>
$(document).ready(function(){
var continuar = true;
var positionNumber = 0;
var flagPressedButton = false;
var i=0;
$(".clickme").click(function(event){
for(i; i < 10;){
ajaxOne();
i++;
if(continuar == false) break;
}
$("body").on('click', '.btn-default', function(e) {
console.log('ok we finish no more calls to servlets');
$('#myModal').modal('hide') ;
console.log('BUT LETS CONTINUE WITH THE FOR.... WE HAVE TO CHECK ALL THE ITEMS')
$(".clickme").click();
});
$("body").on('click', '.btn-primary', function(e) {
console.log('ok lets call now ajax three');
ajaxThree();
$(".clickme").click();
});
});
function ajaxOne(){
$.ajax({
type: "POST",
url: "/Test/one",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
continuar =true;
positionNumber++;
console.log('CALLING POSITION: '+positionNumber);
console.log('Call Servlet one')
},
success: function(data,status,xhr){
console.log('complete: ', JSON.parse(data));
var statusCode = JSON.parse(data).responseCode;
console.log('satusCode: ', statusCode);
if (statusCode) {
ajaxTwo(statusCode);
}
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
}
});
}
function ajaxTwo(){
var text;
$.ajax({
type: "POST",
url: "/Test/two",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet two')
},
success: function(data,status,xhr){
console.log(data);
text = data;
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet two')
if(parseInt(text)==2){
console.error('LLAMARE A LA MODAL');
$('#myModal').modal('show') ;
continuar = false;
}else{
ajaxFour();
}
}
});
}
function ajaxThree(){
$.ajax({
type: "POST",
url: "/Test/three",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet three')
},
success: function(data,status,xhr){
console.log(data);
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet three')
console.error('CERRARE A LA MODALA Y CONTINUARE');
$('#myModal').modal('hide') ;
ajaxFour();
continuar = true;
}
});
}
function ajaxFour(){
$.ajax({
type: "POST",
url: "/Test/four",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet four')
},
success: function(data,status,xhr){
console.log(data);
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet four')
}
});
}
});
</script>
$(文档).ready(函数(){
var continuar=真;
var positionNumber=0;
var flagPressedButton=假;
var i=0;
$(“.clickme”)。单击(函数(事件){
对于(i;i<10;){
ajaxOne();
i++;
如果(continuar==false)中断;
}
$(“正文”)。在('click','btn default',函数(e){
log('好的,我们不再完成对servlet的调用');
$('#myModal').modal('hide');
log('但让我们继续FOR…,我们必须检查所有项目')
$(“.clickme”).click();
});
$(“正文”)。在('click','btn primary',函数(e){
log('ok让我们现在调用ajaxthree');
ajaxThree();
$(“.clickme”).click();
});
});
功能ajaxOne(){
$.ajax({
类型:“POST”,
url:“/Test/one”,
//contentType:“text/xml”,
async:false,
beforeSend:函数(){
continuar=true;
位置号++;
console.log('调用位置:'+位置编号);
log('callservlet one')
},
成功:功能(数据、状态、xhr){
log('complete:',JSON.parse(data));
var statusCode=JSON.parse(data).responseCode;
log('satusCode:',状态码);
if(状态代码){
Ajax2(状态代码);
}
},
错误:函数(xhr、状态、错误){
警报(“错误!”+xhr.status);
}
});
}
函数ajaxTwo(){
var文本;
$.ajax({
类型:“POST”,
url:“/Test/two”,
//contentType:“text/xml”,
async:false,
beforeSend:函数(){
log('调用Servlet二')
},
成功:功能(数据、状态、xhr){
控制台日志(数据);
文本=数据;
},
错误:函数(xhr、状态、错误){
警报(“错误!”+xhr.status);
},
完成:函数(){
log('finishcallservlet二')
if(parseInt(text)==2){
控制台错误('LLAMARE A LA MODAL');
$('myModal').modal('show');
连续=假;
}否则{
ajaxFour();
}
}
});
}
函数ajaxThree(){
$.ajax({
类型:“POST”,
url:“/Test/three”,
//contentType:“text/xml”,
async:false,
beforeSend:函数(){
for(i; i < 10;){
ajaxOne();
i++;
if(continuar == false) break;
<script>
$(document).ready(function(){
var continuar = true;
var positionNumber = 0;
var flagPressedButton = false;
var i=0;
$(".clickme").click(function(event){
for(i; i < 10;){
ajaxOne();
i++;
if(continuar == false) break;
}
$("body").on('click', '.btn-default', function(e) {
console.log('ok we finish no more calls to servlets');
$('#myModal').modal('hide') ;
console.log('BUT LETS CONTINUE WITH THE FOR.... WE HAVE TO CHECK ALL THE ITEMS')
$(".clickme").click();
});
$("body").on('click', '.btn-primary', function(e) {
console.log('ok lets call now ajax three');
ajaxThree();
$(".clickme").click();
});
});
function ajaxOne(){
$.ajax({
type: "POST",
url: "/Test/one",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
continuar =true;
positionNumber++;
console.log('CALLING POSITION: '+positionNumber);
console.log('Call Servlet one')
},
success: function(data,status,xhr){
console.log('complete: ', JSON.parse(data));
var statusCode = JSON.parse(data).responseCode;
console.log('satusCode: ', statusCode);
if (statusCode) {
ajaxTwo(statusCode);
}
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
}
});
}
function ajaxTwo(){
var text;
$.ajax({
type: "POST",
url: "/Test/two",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet two')
},
success: function(data,status,xhr){
console.log(data);
text = data;
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet two')
if(parseInt(text)==2){
console.error('LLAMARE A LA MODAL');
$('#myModal').modal('show') ;
continuar = false;
}else{
ajaxFour();
}
}
});
}
function ajaxThree(){
$.ajax({
type: "POST",
url: "/Test/three",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet three')
},
success: function(data,status,xhr){
console.log(data);
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet three')
console.error('CERRARE A LA MODALA Y CONTINUARE');
$('#myModal').modal('hide') ;
ajaxFour();
continuar = true;
}
});
}
function ajaxFour(){
$.ajax({
type: "POST",
url: "/Test/four",
//contentType : "text/xml",
async :false ,
beforeSend: function () {
console.log('Call Servlet four')
},
success: function(data,status,xhr){
console.log(data);
},
error: function(xhr, status, error){
alert("Error!" + xhr.status);
},
complete: function(){
console.log('finish Call Servlet four')
}
});
}
});
</script>