Javascript 如何在 $.post jQuery 中等待响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10575214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to wait for response in $.post jQuery
提问by heron
PHP returns value with 1-2 second delay jQuery.post doesn't wait for response.
PHP 以 1-2 秒的延迟返回值 jQuery.post 不等待响应。
How do you think, is it possible to fix that problem and wait for response?
您认为如何解决该问题并等待响应?
$.post( sSource, aoData, function (data) {
oCache.lastJson = jQuery.extend(true, {}, data);
if ( oCache.iCacheLower != oCache.iDisplayStart )
{
data.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
}
data.aaData.splice( oCache.iDisplayLength, data.aaData.length );
abc(oCache);
fnCallback(data);
},"json" );
Notethe same function with get works well
请注意与 get 相同的功能效果很好
$.getJSON( sSource, aoData, function (json) {
/* Callback processing */
oCache.lastJson = jQuery.extend(true, {}, json);
if ( oCache.iCacheLower != oCache.iDisplayStart )
{
json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
}
json.aaData.splice( oCache.iDisplayLength, json.aaData.length );
fnCallback(json)
} );
回答by Vitaly
$.post is asynchronous, you need to use $.ajax and set async to false, that way you will be able to wait for the response. You can read more about it here: http://api.jquery.com/jQuery.ajax/
$.post 是异步的,您需要使用 $.ajax 并将 async 设置为 false,这样您就可以等待响应。您可以在此处阅读更多相关信息:http: //api.jquery.com/jQuery.ajax/