[jQuery] jQuery.ajax(options)、$.ajax のメモ
- 2013 9/27
jQuery の $.ajax メソッドの引数や、success, error, complete などに指定する引数をよく忘れてしまうのでメモ。
jQuery 1.7 までの $.ajax
$.ajax({ url: 'http://api.example.com/v1/posts', type: 'POST', dataType: 'HTML', data: { id: '123' }, success: function(data, statusText, xhr){ if (xhr.status === 200) { // 成功時の処理 } else if (xhr.status === 302) { // HTTPステータスコードによって、処理を分岐 // 302 でリダイレクトさせたり location.href = 'http://example.com/redirect'; } }, error: function(xhr, statusText, error) { // エラー処理 }, complete: function(xhr, statusText){ // 共通処理 } }); |
jQuery 1.8 以降の $.ajax
success, error, complete が .done, .fail, .always に変更されているようです。
これについては、いつか更新します。
参考情報
・jQuery モダンAjaxな書き方を目指して 〜Deferredを使ったAJAX〜 – Hack Your Design!