jQuery Ajax失败并返回异常? [英] JQuery Ajax fail and return exception?
问题描述
我已经阅读了几篇有关JQuery Ajax调用失败参数的文章,但没有一篇直接回答我的问题.如果您想在这里阅读我的出发点,那么这篇文章将是一个不错的开始:
I have read a few posts on fail parameters for a JQuery Ajax call, but none that have directly answered my question. If you want to read up on my jumping off point here, this post would be a good start:
jquery:是否有$的失败处理程序.post在Jquery中?
我的问题是,有一些事情可能导致我的应用程序失败-如果我的脚本返回false,则上述方法对我来说就可以工作(如果我正确理解的话),但是大多数情况下,我的脚本会因为踢出我使用Zend Framework处理的异常而失败.我希望返回该异常,以便为用户提供更详细的消息.可以让我的PHP脚本返回一个值,同时仍然让Ajax调用知道这是一个失败吗?
My problem is that there are a handful of things that may cause my application to fail - if my script returns false the above method would work just fine for me (if I understand it correctly), but most of the time my script will fail by kicking out an exception that I handle using the Zend Framework. I would prefer to return the exception so that I can provide the user with a more detailed message. Is it possible to have my PHP script return a value while still letting the Ajax call know that it was a failure?
推荐答案
可以.首先,您需要对错误进行分类,例如:
Sure you can. First of all you need to categorize you errors, for example:
- 致命
- 例外
- 错误/错误状态
我建议您将返回值用于正确且无错误的处理-0 .在所有其他情况中,这都是错误.
I would advise you to take as a return value for correct and with no errors processing - 0. In all other case that would be an error.
另一个有用的建议是将JSON用作客户端-服务器对话.
Another useful advise would be to use JSON as a client-server conversation.
在PHP中,它将是:
function prepareJSONResponse($code, $message, array $extra = array())
{
return json_encode(array_merge(
$extra, array(
'code' => (int) $code,
'message' => $message)));
}
在这种情况下,您可以为此调用传递错误代码和消息以及$ extra中的其他参数:
In this case you could pass error code and message, and additional params in $extra, for example, for this call:
prepareJSONResponse(1, 'Not enough data passed', array('debug' => true));
来自服务器端的响应将是:
Response from server side would be:
{code:1,message:'Not enough data passed','debug': true}
对于客户端,您需要$ .ajax的包装函数:
For the client side you need a wrapper function for $.ajax:
// calback(result, error);
function call(url, params, callback)
{
if (typeof params == 'undefined') {
params = {};
}
$.ajax({
'type' : "POST",
'url' : url,
'async' : true,
'data' : params,
'complete' : function(xhr) {
if (xhr.status != 200) {
if (typeof callback == 'function') {
callback(xhr.responseText, true);
}
} else {
if (typeof callback == 'function') {
callback(xhr.responseText, false);
}
}
}
});
}
并具有验证JSON的功能,以便确定格式是否损坏.
and function to validate JSON, in order if the corrupted format comes.
function toJSON(data){
try {
data = JSON.parse(data);
} catch (err) {
data = { 'code' : -999, 'message' : 'Error while processing response' };
}
if (typeof data.debug != 'undefined') {
console.log(data.debug);
}
return data;
}
在try-catch中包装您的代码,然后在catch语句中执行以下操作:
Wrap in try-catch you code, and in catch statement do something like:
try {
...
} catch (Exception $e) {
exit(prepareJSONResponse(1, $e->getMessage(), array(
'debug' => 'There was an error while I were processing your request')));
}
结果将是您在浏览器控制台中收到调试信息,并且可以处理错误/异常(prepareJSONResponse())和致命错误(通过读取HTTP状态标头,如果不是200,则表示错误).
The result would be that you receive debug information in browser console, and could handle errors / exception (prepareJSONResponse()) and fatals (by reading HTTP-status header, if it's not 200, then there was error).
希望这就是您的要求.
这篇关于jQuery Ajax失败并返回异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!