如果将json_encode应用于输出,则不会显示异常消息 [英] Exception message not being shown if json_encode is applied to the output
本文介绍了如果将json_encode应用于输出,则不会显示异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码:
try{
//The exception is thrown.
throw new Exception('Parâmetros de consulta inválidos');
// and in the catch block it's caught successfully:
}catch(Exception $e){
echo $e->getMessage(); //This prints the message correctly.
$output = json_encode(array('msg'=>$e->getMessage()));
echo $output; //But this fails...displays {"msg":null}
}
这是什么问题?
推荐答案
问题在于字符â和á.实际上,如果将它们替换为简单的a,您将获得正确的消息.
The problem is with the character â and á. Infact, if you replace them with a simple a, you'll get the right message.
替换此行:
array('msg'=>$e->getMessage())
与此:
array('msg'=>utf8_encode($e->getMessage()))
您必须进行此更改,因为json_encode可与ut8一起使用,因为您可以阅读这里.
You have to do this change because the json_encode works with ut8 as you can read here.
这篇关于如果将json_encode应用于输出,则不会显示异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文