Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Created November 21, 2013 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoyamkung/7575105 to your computer and use it in GitHub Desktop.
Save tomoyamkung/7575105 to your computer and use it in GitHub Desktop.
[JavaScript]Ajax 通信処理で失敗した responseText をログ出力する。
/**
* Ajax 通信処理で失敗した responseText をログ出力する。
*
* responseText には次の形式の JSON が設定されている想定。
*
* {
* "errors" : [
* エラーメッセージ1,
* エラーメッセージ2,
* エラーメッセージ3
* ]
* }
*
* @param {XMLHttpRequest} xhr Ajax 処理失敗時に返ってくる XMLHttpRequest オブジェクト
*
*/
function showErrorMessage(xhr) {
if(typeof window.console === "undefined") {
return;
}
var json = JSON.parse(xhr.responseText);
for(var i = 0, len = json.errors.length; i < len; i++) {
console.log(json.errors[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment