使用Ajax进行异步PHP查询?

我无法理解这一点。 Ajax是异步的,可以肯定的是......它异步调用PHP,并且PHP对数据库有一个sql查询。 这意味着PHP sql查询也是异步完成的,对吧? 否则,它就会失败。 但是不使用Ajax,PHP sql查询会是同步的,是吗? 我知道如何把它放到代码中,但我只是对内部的工作方式感到困惑。</ p>
</ div>

展开原文

原文

I'm having trouble understand this. Ajax is asynchronous, that much is certain...it asynchronously calls on PHP, and that PHP has a sql query for the database. That means that PHP sql query is also done asynchronously, right? Otherwise, it defeats the purpose. But without using Ajax, the PHP sql query would be synchronous, is that it? I know how to put it to code, but I'm just confused on how it works internally.

duanlieshuang5330
duanlieshuang5330 这使得现在更加清晰。谢谢。您是否介意将您的评论作为答案,以便我能正确地给予它评价?
接近 8 年之前 回复
dongyuqi3808
dongyuqi3808 所以不,PHPSQL查询始终是同步的,它是异步的HTTP请求。
接近 8 年之前 回复
dpa0760
dpa0760 SQL查询是“异步的”,因为它是原始AJAX调用的一部分。使用AJAX的“异步”的含义是它从Javascript的其余部分异步处理-它不会阻止其他代码执行。但从那时起,一切都必须是同步的,否则它将无法工作(不包括AJAX的readystatechange)。AJAX请求进入服务器,服务器查询数据库,服务器响应,Javascript听到响应,AJAX一旦没有被阻止就处理响应。
接近 8 年之前 回复

2个回答

The SQL query is "asynchronous" in terms of it being apart of the original AJAX call. Don't get hung up on the terminology past that. Client and server are totally separate, especially when dealing with HTTP requests, and in this scenario. The meaning of "asynchronous" with AJAX is that it is processed asynchronously from the rest of the Javascript - it doesn't block the other code from executing. But from then on, everything must be synchronous, otherwise it will not work (excluding the readystatechange for the AJAX).

The AJAX request goes to the server, the server queries the database, the server responds, the Javascript hears that response, and the AJAX handler processes the response as soon as it isn't being blocked by other Javascript.

So no, the PHP SQL query itself is always synchronous; it's the HTTP request that is asynchronous.

UPDATE:

As an example, here's a very stripped, low level of AJAX that most libraries wrap in certain ways:

var xhr= new XMLHttpRequest();
var params = "x=2&y=3";
var url = "/your/url";
xhr.open("POST", url, true);
xhr.onreadystatechange = function () {
    // The `xhr.readyState` changes based on the client's 
    // The `xhr.status` is set based on the server's response
    // Normally, you check for `readyState` being 4 and `status` being 200
    //   meaning that the request is complete and the HTTP status code is 200 (good response)
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            // All good
        }
    } else {

    }
};
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(encodeURIComponent(params));

When xhr.send is called, it makes the asynchronous HTTP request to the server, and that's just about that for the Javascript. The onreadystatechange listener is what runs when the readyState changes. A value of 4 means it has completed, and status 200 is what you're looking for...whenever that is.

Anything can happen on the server. You can make an "asynchronous" (unrelated) database call, contact a different server in whatever way, delay for whatever reason (to a certain limit) or something like that. The point is that nothing is known on the client (Javascript) until the server returns a response. The server could loop for a long time checking the database each time, and never respond until there's a certain change (an example of long polling).

dtrj74376
dtrj74376 完美答案。 谢谢
接近 8 年之前 回复



好的,你是非常正确的,但这里有一些规范。 您对数据库的查询是异步的权利。 请务必注意,您的PHP服务器将与客户端计算机创建一个开放且唯一的数据会话。 现在,当请求页面时,客户端将发送一个响应,其中包含将在客户端计算机上运行命令的脚本(可能在javascript中)。 在您的情况下,其中一些命令会对您的服务器进行异步调用,并且您的服务器可以因为会话而记住。 然后根据您的服务器对会话的了解,它将根据您的请求提供呼叫。 您可以调用数据库服务器或访问会话变量,或者在PHP中执行某些可能更容易的处理。 然后,当完成该处理时,服务器将基于唯一会话将响应发送回相关客户端。 作为一个重要的注意事项,请记住,异步操作将花费时间在服务器上并执行它必须执行的操作。 如果您在客户端脚本中引用服务器在完成其工作之前处理的变量,它们将在您的客户端计算机上显示为错误(可能但非常可能)。 您的数据库调用将按预期异步发生。 </ p>
</ div>

展开原文

原文

Okay, you're pretty much correct, but here is some specification. Your right that a query to your database will be asynchronous. It is important to note that your PHP server will create an open and unique data session with the client machine. Now, when a page is requested, the client will send a response that will include a script (probably in javascript) that will run commands on the client machine. In your case some of those commands make asynch calls to your server, and your server can remember because of the session. Then based on what your server knows about the session it will serve the calls as you request. You can call your database server or access session variables or do some kind of processesing that might be easier in PHP. Then when that processing is done the server will send a response back to the client in question based on the unique session. As an important side note remember that what ever you do asynchronously will take its time on the server and do what it has to do. If you reference variables in your client scripts that where being processed by the server before it finishes its work, they will appear on your client machine as wrong (potentially but very likely). Your database calls will happen asynchronously as you expected.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐