繁体   English   中英

AJAX加载中的Javascript块

[英]Javascript block on AJAX load

我正在寻找一种在javascript中提出AJAX加载请求的方法,但是在等待AJAX​​加载完成时让javascript代码暂停执行。 换句话说,我正在尝试执行同步 AJAX加载请求(我知道AJAX中的'A'代表异步。我只是希望名称不完全正确。)。 我有的是

$('#my_id').load("my_page.pl?my_param=p1&my_other_param=p2");
//Please wait here
//Now do stuff after load is complete.

我希望请求是同步的,原因是它创建了一个HTML表,然后后面的JavaScript解析了该表。

jQuery的“ load()”方法进行回调。 回调通常是异步代码处理所需的“等待”功能的方式。

$("#my_id").load("my_page.pl?my_param=p1&my_other_param=p2", function (response) {
    // do this stuff, which will run after the request is complete
});

看起来像jquery ...我不确定,因为我根本不了解jquery,但是如果这样的话,您可能会得到更好的标记它的答案。 我可以告诉您javascript有自己的同步或异步调用方法。 这是我用的:

var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
}
else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// Use the activeX object for old IE

xmlhttp.open("GET","http://some.internet.address/woot?foo=bar", false);
// The third argument in the open function determines the asynchronous nature, 
//it was set to false, so the code will halt at the send() point while the request is executed.
xmlhttp.send();
// Now, if the call succeeded...
if (xmlhttp.status==200) {
    response = eval('('+xmlhttp.responseText+')');
    // ...save the results to an object named response...
}
else {
    // ...Otherwise do some other stuff
    errorFunction(xmlhttp.status);
}

您也可以在这里找到很多很好的信息-http: //www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM