是否需要这两个请求取决于您的用例。既然你的听起来确实很有必要,那绝对没有错。
如何在客户端处理服务器端错误也完全取决于您。例如,让我们假设这个非常基本的 XHR 处理程序,它将一些 JSON 数据发送到某个端点并评估响应:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/endpoint.php');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
const status = xhr.status;
const response = JSON.parse(xhr.responseText);
if (status == 200) {
// Everything's fine
console.log(response.data);
} else {
// Some error occured
console.log(status, response.data);
}
};
xhr.send(JSON.stringify({}));
index.html
上述错误处理策略围绕着从服务器接收到的 HTTP 状态码,所以我们需要确保它们是根据我们的需要发送的:
/**
* In case an error occurred, send an error response.
*/
function error(string $message) {
// For properly returning a JSON response, see:
// https://stackoverflow.com/a/62834046/3323348
header("Content-type: application/json; charset=utf-8");
http_response_code(400); // HTTP status code - set to whatever code's appropriate
echo json_encode(['data' => $message]);
}
/**
* Send a response denoting a success.
*/
function success(array $data) {
// For properly returning a JSON response, see:
// https://stackoverflow.com/a/62834046/3323348
header("Content-type: application/json; charset=utf-8");
http_response_code(200);
echo json_encode(['data' => $data]);
}
// For proper error checking on JSON data, see:
// https://stackoverflow.com/a/37847337/3323348
$data = json_decode(file_get_contents('php://input'), true);
// Assume some processing happened. That result is either
// a success, or an error.
$success = mt_rand(0,1);
$success ? success($data) : error('An error occured.');
endpoint.php
关于 cURL,您可以轻松地将最后两行更改为例如:
if(curl_errno($ch)) { // Assuming $ch is your cURL handle
error('Curl error: '.curl_error($ch));
}
else {
success($data);
}
或者你修改错误函数——如果它接受一个数组而不是一个简单的字符串,你就可以向你的 JavaScript 客户端返回更复杂的错误数据:
$curl_error = curl_errno($ch);
if ($curl_error) {
error([
'type' => 'cURL',
'no' => curl_errno($ch),
'message' => curl_error($ch)
]);
}
不过,您不必坚持使用状态代码来处理错误。您可以返回一个 JSON 对象,其中一个键表示一些成功收集的数据,如果发生错误,则一个表示错误。或任何适合您的需求。