Skip to content
Go back

接口状态码

Edit

最近和团队讨论接口返回格式时,遇到了一个很常见但容易纠结的小问题:

成功状态是 success 还是 succeeded
失败状态是 failure 还是 failed

简单整理一下词性和实际用法,结论其实很清晰。

词性分析

业界常见写法

1. success: true/false(最流行之一,尤其通用中后台/内部 API)

// 成功
{
  "success": true,
  "data": {
    "id": 123,
    "name": "Alice"
  },
  "message": "操作成功"   // 可选
}
// 失败(HTTP 200)
{
  "success": false,
  "error": {
    "code": "INVALID_PARAM",
    "message": "用户名不能为空"
  },
  "data": null             // 可选
}

2. status: “succeeded” / “failed”(支付、订单、任务、异步操作最常见)

// 成功
{
  "status": "succeeded",
  "data": {
    "transaction_id": "txn_abc123",
    "amount": 99.99
  }
}
// 失败(HTTP 200)
{
  "status": "failed",
  "error": {
    "code": "INSUFFICIENT_BALANCE",
    "message": "余额不足"
  }
}

3. status: “success” / “failure”(正式感强,企业级规范常见)

// 成功
{
  "status": "success",
  "data": {
    "user": {
      "id": 456,
      "email": "test@example.com"
    }
  }
}
// 失败(HTTP 200)
{
  "status": "failure",
  "error": {
    "code": 4001,
    "message": "参数验证失败"
  }
}

4. status: “ok” / “error”(极简风格,很多内部系统/新兴 API)

// 成功
{
  "status": "ok",
  "data": {
    "items": ["apple", "banana"]
  }
}
// 失败
{
  "status": "error",
  "message": "服务器内部错误",
  "code": 500
}

5. 纯 error 对象 + data 存在与否判断(纯 REST 风格,越来越流行)

// 成功(HTTP 200/201)
{
  "data": {
    "id": 789,
    "title": "New Post"
  }
}
// 失败(HTTP 400/422 等)
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "标题长度不能超过 100 字符",
    "details": ["title: too long"]
  }
}

6. 带 meta 的更完整结构(大型项目/分页场景常见)

// 成功
{
  "success": true,
  "data": [...],
  "meta": {
    "total": 100,
    "page": 1,
    "per_page": 20
  }
}
// 失败
{
  "success": false,
  "error": {
    "code": "AUTH_FAILED",
    "message": "Token 已过期"
  },
  "meta": {
    "timestamp": "2026-02-10T12:47:00Z"
  }
}

推荐

// 成功
{
  "success": true,
  "data": { ... }
}
// 失败(HTTP 200)
{
  "success": false,
  "error": {
    "code": "INVALID_PARAM",
    "message": "参数错误"
  }
}
// 成功
{
  "status": "succeeded",
  "data": { ... }
}
// 失败(HTTP 200)
{
  "status": "failed",
  "error": { ... }
}

Edit
Share this post on:

Next Post
占位图和 JSON Mock 服务对比