【问题标题】:PHP Tools for Visual Studio. How to debug AJAX requests?适用于 Visual Studio 的 PHP 工具。如何调试 AJAX 请求?
【发布时间】:2015-05-18 05:40:25
【问题描述】:

我有一个关于 PHP 的小项目,我想使用 PHP Tools for Visual Studio 对其进行调试。它工作正常,调试器工作得很好。但我的项目中有一部分作为服务工作,它监听来自 html 页面的 AJAX 请求并以 JSON 格式发送响应。在这种情况下,调试器根本不起作用。我在我的 php 服务文件中设置了断点,它永远不会触发,但我在客户端得到了正确的响应。所以,我的问题是如何使用 PHP 工具调试 AJAX 请求?

我以我的客户端和服务器代码为例来说明我的问题。

server.php

<?php

if(isset($_REQUEST['response'])) {
    echo json_encode('ok'); 
}
else {
    echo json_encode('cancel'); 
}

?>

client.html

<!DOCTYPE HTML>
<html>
<head>
   <title>AJAX test</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

   <script type="text/javascript">
    $( document ).ready(function() {
        $( "#request" ).bind( "click", function() {
            var data = { response: "response" };

            $.ajax({
                url: "server.php",
                type: "POST",
                async: false,
                timeout: 5000,
                dataType: "json",
                data: data,
                success: function(result) {
                    $("#response").text(result);
                },
                error: function(error) {
                    $("#response").html(error.responseText);
                }
            }); 
        });
    });

  </script>
</head>

<body>
   <p>
     <b>Request: </b> <span id="request"><a href="#">Send request</a></span>
   </p>
   <p>
     <b>Response: </b> <span id="response">Here will be your response...</span>
   </p>
</body>

</html>

所以当我在 server.php 中的 if-statement 行设置断点时,它不会触发,但客户端成功接收服务器响应。

【问题讨论】:

    标签: php ajax debugging visual-studio-2012


    【解决方案1】:

    我在DEVSENSE forum 上找到了解决方案 在这种情况下,xdebug 只是不知道什么时候应该开始调试。简单的方法是添加到 php.ini 下一行:

    xdebug.remote_autostart = on
    

    一切正常!

    【讨论】:

    • 请注意 Xdebug v3 no longer needs(支持?)remote_autostart。链接的升级文档说“The xdebug.remote_autostart setting has been removed. Instead, set xdebug.start_with_request to yes
    • 文档链接(Xdebug 2 和 3)-docs.devsense.com/en/vs/debugging/…
    猜你喜欢
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2018-11-27
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多