第二个ajax调用不工作,第二个应该在第一个调用成功后才调用
Second ajax call is not working , the second one should be called only after the first one is sucess
我有两个ajax调用,第二个应该在第一个ajax成功后才触发。每次第一个ajax返回成功,但我总是得到错误的第二个。
function proceedWithUnlock(target, address, PARAMS) {
target.style.backgroundColor = 'yellow';
target.nextElementSibling.innerHTML = 'processing....';
target.nextElementSibling.style.backgroundColor = 'yellow';
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText.match('Success')) {
target.nextElementSibling.innerHTML = 'Success!!!';
target.nextElementSibling.style.backgroundColor = 'lightgreen';
lockTheNumber(target, address1, PARAMS1);
} else {
target.nextElementSibling.innerHTML = 'FAILED';
target.style.backgroundColor = 'red';
target.nextElementSibling.style.backgroundColor = 'red';
}
} else {
target.nextElementSibling.innerHTML = 'FAILED';
target.style.backgroundColor = 'red';
target.nextElementSibling.style.backgroundColor = 'red';
}
}
}
client.open("POST", address);
client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
client.send(PARAMS);
}
function lockTheNumber(target, address1, PARAMS1) {
target.nextElementSibling.nextElementSibling.innerHTML = 'processing....';
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'yellow';
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText.match('Success')) {
target.nextElementSibling.nextElementSibling.innerHTML = 'Success11111!!!';
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'lightgreen';
target.style.backgroundColor = 'lightgreen';
} else {
target.nextElementSibling.nextElementSibling.innerHTML = 'FAILED 1';
target.style.backgroundColor = 'red';
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'red';
}
} else {
target.nextElementSibling.nextElementSibling.innerHTML = 'UNABLE TO BLOCK 2';
target.style.backgroundColor = 'red';
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'red';
}
}
}
client.open("POST", address);
client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
client.send(PARAMS);
}
错误信息:"Error in completion operation "
创建一个新的XMLHttpRequest对象,而不是重用旧对象。它超出了范围。
...
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'yellow';
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
...
您的另一个XMLHttpRequest
对象超出范围。您需要在第二个函数中创建一个新对象:
function lockTheNumber(target, address1, PARAMS1){
target.nextElementSibling.nextElementSibling.innerHTML = 'processing....';
target.nextElementSibling.nextElementSibling.style.backgroundColor = 'yellow';
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
您已经添加了jquery
标记,因此我认为您也可以利用该库。JQuery
支持承诺,或者在JQuery
语法中它被称为延迟。
这有助于用then
操作符构建ajax调用更多信息见文档:https://api.jquery.com/deferred.then/
JQuery还提供了一个很好的ajax调用包装器
相关文章:
- IE11中的第二个调用取消了第一个Fetch API调用
- 如何正确地将第二个变量传递给ajax调用
- 当第二个参数为空时管理JavaScript调用的构造
- 第二个Ajax调用已经填充了成功结果
- onKeyUp在按下第二个按钮时调用
- 从一个页面调用 javascript 函数以在第二个页面上添加元素
- 角度$scope首先设置值而不是服务,然后第二个函数调用一切正常
- 如何在第一个 php 脚本中访问变量 'a' 的值到第二个 php 脚本,并通过 ajax 调用链接
- AngularJS + Ionic - 如何从第一个控制器调用第二个控制器上的方法
- 如何使用 Nodejs、Expressjs 和 bluebird 调用第二个“外部”函数
- 在调用第二个函数后启用字段函数触发器
- 如何在第一个函数完全执行后调用第二个函数
- 在 StorageArea.clear 之后未调用第二个警报
- 完成第一个动画后,调用第二个动画
- 我该如何调用第二个函数来操作点击
- 按要求设置对象.在调用第二个中间件时,两个中间件中的第一个似乎不存在
- 当onclick事件处理机制启动时,为什么不调用第二个函数?
- 在Javascript中传递函数到函数会立即调用第二个函数
- 如何在Wordpress中排队并调用第二个JQuery版本
- 如何在执行第一个函数之后调用第二个函数