doufenzhu7012 2012-12-18 01:41
浏览 104
已采纳

使用Ajax进行异步PHP查询?

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.

  • 写回答

2条回答 默认 最新

  • dpwu16132 2012-12-18 02:45
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图