简体   繁体   English

Ajax无法以同步方式工作

[英]Ajax not working in synchronous manner

I am using prototype.js for making Ajax request. 我正在使用prototype.js发出Ajax请求。 Everything was working fine till I changed asynchronous to false (As I want to make synchronous requests). 一切正常,直到我将asynchronous更改为false (因为我想发出同步请求)。 Following are the few lines from my code. 以下是我的代码中的几行内容。

    var commOptions = {
                contentType: 'text/xml',
                method: 'post',
                asynchronous: true,
                evalJS: false,
                evalJSON: false
            };   
   sProxy_Url = sUrl;  
   new Ajax.Request(sProxy_Url, commOptions);   

If I change asynchronous to false then Ajax do not work. 如果我将asynchronous更改为false那么Ajax将无法正常工作。 Am I missing anything ? 我有什么想念的吗?

Honestly I don't see what's not working, I even got the response in javascript to make sure: 老实说,我看不到有什么问题,我什至在javascript中也得到了响应,以确保:

var commOptions = {
                contentType: 'text/xml',
                method: 'post',
                asynchronous: false,
                evalJS: false,
                evalJSON: false
            };   

   var a = new Ajax.Request("/", commOptions);
console.log( a.transport.responseText.length ); //16898

http://jsfiddle.net/HRu78/1/ http://jsfiddle.net/HRu78/1/

It's just about always recommended to make ajax requests asynchronous since making them synchronous will block the main JavaScript thread, which provides a poor user experience (see this ). 几乎总是建议使ajax请求异步,因为使它们同步将阻塞主JavaScript线程,这会带来糟糕的用户体验(请参阅参考资料 )。

However, if you really want to use the synchronous option you'll need to be a bit more clear about what's happening when you run the code - does the browser block? 但是,如果您真的想使用sync选项,则需要更清楚地了解运行代码时发生的情况-浏览器是否阻塞? Or does the function return but with no data? 还是函数返回但没有数据?

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

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