【发布时间】:2025-11-30 19:20:21
【问题描述】:
我想从 PHP API 处理器脚本发回 JSON。对于错误消息,我像这样访问它:
try {
$tax = $client->taxForOrder([
"field"=>($value),
"field"=>($value),
//etc
);
$data = $tax;
//json encoding works here
echo json_encode($data);
}
catch (TaxJar\Exception $e) {
// 406 Not Acceptable – transaction_id is missing
echo $e->getMessage();
// 406
echo $e->getStatusCode();
}
通过上述内容,它按预期返回纯文本“400 Bad Request – to_zip 66762 is not used within to_state NJ400”。但我想要 JSON,因为 jQuery $.ajax 成功函数正在寻找“JSON”的数据类型。 当我尝试时:
$msg = $e->getMessage();
echo json_encode($msg);
它不返回任何东西。如何正确获得类似 {"error","msg":"400 Bad Request..."}
【问题讨论】: