简体   繁体   English

如何在 Ajax 调用中正确捕获 PHP 错误

[英]How to Properly Catch a PHP error on Ajax call

I am retrieving javascript code from the server via the following ajax call我正在通过以下 ajax 调用从服务器检索 javascript 代码

ajax (dojo): ajax(道场):

dojo.xhrGet({
    url : 'script.php',
    handleAs : "javascript",
    load : function(response){
        /*Do Something*/
    },
    error : function(errorMessage) {
        console.error(errorMessage);
    }
});

script.php works fine, and, if the javascript code it returns is not valid code, the error handler will be invoked. script.php 工作正常,如果它返回的 javascript 代码不是有效代码,则会调用错误处理程序。 However, the error message is incomplete, ie.但是,错误消息是不完整的,即。 it only shows the last function the error occurred in, not the entire chain of function calls.它只显示最后一个 function 发生的错误,而不是 function 调用的整个链。 This is at times not very useful as I want to know where the error originated.这有时不是很有用,因为我想知道错误的来源。 Is there any way to output the entire trace?有没有办法 output 整个跟踪?

Post the response of what you are returning.发布您返回的内容的响应。

I think you are better trying with a JSON response.我认为您最好尝试使用 JSON 响应。

I was getting the same error object on xhrPost for a _saveCustom, even though my response status was 200 (the 'handle:' is the same for GET and POST).我在 xhrPost 上为 _saveCustom 收到相同的错误 object,即使我的响应状态为 200(GET 和 POST 的“句柄:”相同)。

Actually handle: as 'json' will threw the error as well, but handle as 'text' worked and triggered the call to the load callback function.实际上句柄:as 'json' 也会抛出错误,但是句柄 as 'text' 起作用并触发了对加载回调 function 的调用。

The reason I wasn't getting reliable error information is because I was using eval and I was expecting the same behavior I would observe having instead included those scripts.我没有得到可靠的错误信息的原因是因为我正在使用eval并且我期待与我观察到的相同行为,而不是包含这些脚本。 This question describes the differences and how eval runs the code whereas including those codes first inserts the code into the DOM then runs the code. 这个问题描述了差异以及eval如何运行代码,而包含这些代码首先将代码插入 DOM ,然后运行代码。 The former is more efficient, but the latter is easier to debug.前者效率更高,但后者更容易调试。

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

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