0

それは非常に簡単です。HTTP 412 とエラーの JSON の説明を返す API があります。

Status Code:412 PRECONDITION FAILED

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8000
Access-Control-Expose-Headers:access-token, expiry, uid
Content-Length:224
Content-Type:application/json
Date:Wed, 09 Mar 2016 16:31:39 GMT
Server:Werkzeug/0.9.6 Python/3.5.1
{
  "errors": {
    "email": [
      "This field is required."
    ],
    "password": [
      "String value is too short."
    ],
    "password_confirmation": [
      "doesn't match Password"
    ]
  },
  "status": "error"
}

私のjavascript(ES6)で私のURLを取得し、必要な両方の情報を把握することは可能ですか:

  • HTTP コード
  • 解析されたJson
export function checkHttpStatus(response) {
  console.log(response);
  if (response.status >= 200 && response.status < 300) {
    return response;
  }
  throw error;
}
export function parseJSON(response) {
  return response.json();
}
return fetch(getRegisterUrl(), {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(body)
})
.then(checkHttpStatus)
.then(parseJSON);

ヘッダーの前にJSONを解析すると、response.statusはもう利用できません。json の前にヘッダーを解析すると、JSON 本体を取得する前に例外がスローされます。

これを処理する方法はありますか?

4

0 に答える 0