如何同步请求4个ajax,AJAX:我如何解决需要同步请求的问题?

我删除了一个以前提出的问题,现在再次更简洁地问我对问题有更多的理解。AJAX:我如何解决需要同步请求的问题?

我有一个页面,使用大量的AJAX,并更新和动态填充内容(表单元素和div从其他表单元素返回的文本)。

我真的需要能够强制用户等待,直到AJAX请求处理完毕(例如,在他们完成提交一些值的文本字段后),然后才能继续。从我收集使用同步请求可能会导致浏览器挂等问题...

我还需要一个JavaScript函数,执行多个AJAX函数调用,但我也需要他们一次执行一个。目前使用异步调用会产生不可预知的结果......也许我会以错误的方式解决这个问题。有很多的代码,所以很难提供一个简短的例子,但我会试试下面:

从下面(它本身插入一个Ajax函数)动态地添加更多的表单元素到页面if它是第一次输入已被编辑(第一次内容被写入数据库...随后它只会更新现有的数据库条目,我不需要添加额外的表单项)

调用:

function isNewTalkItem (talkID, talkItemID, newTalkItemID, newNoteID){

var url = "./wp-content/themes/twentyten/addBelowIfNew.php";

var poststr = "talkItemID=" + talkItemID;

http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...

http_request = new XMLHttpRequest();

if (http_request.overrideMimeType) {

// set type accordingly to anticipated content type

//http_request.overrideMimeType('text/xml');

http_request.overrideMimeType('text/html');

}

} else if (window.ActiveXObject) { // IE

try {

http_request = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

http_request = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {}

}

}

if (!http_request) {

alert('Cannot create XMLHTTP instance');

return false;

}

http_request.onreadystatechange = function(){

if (http_request.readyState == 4) {

if (http_request.status == 200 || http_request.status == 0) {

result = http_request.responseText;

//following code will note execute if this is not the first time this field has been edited...

if (result) {

// call to AJAX function that inserts a new

newNote(newNoteID, talkItemID);

// another call to AJAX function that inserts a new

newTalkItem(talkItemID, talkID);

}

} else {

alert('There was a problem with the request.');

}

}

}

http_request.open('POST', url, true);

http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http_request.setRequestHeader("Content-length", poststr.length);

http_request.setRequestHeader("Connection", "close");

http_request.send(poststr);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值