From 063baf1a61bb28a5616a3e101650d1894e6a7a17 Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Tue, 13 Aug 2013 12:30:10 +0200 Subject: [PATCH 3/3] Handling more properly json responses --- calebasse/static/js/calebasse.personnes.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/calebasse/static/js/calebasse.personnes.js b/calebasse/static/js/calebasse.personnes.js index 6df9699..afbec6a 100644 --- a/calebasse/static/js/calebasse.personnes.js +++ b/calebasse/static/js/calebasse.personnes.js @@ -28,18 +28,15 @@ function add_holiday(worker) { params = {'title': 'Ajouter une absence', 'button_close': 'Fermer', 'button_confirm': 'Ajouter', 'width': '500px'}; - on_success = function(response, status, xhr) { - var content_type = xhr.getResponseHeader('content-type'); - if(content_type.indexOf('html') > -1) { - $('#holiday-dlg form').html(response); - } else { + on_success = function(response) { + try { + $.parseJSON(response); if(!response.err) { var result = response.content; var id = response.id; var li = $("
  • "); var ul = $(''); $.each(result, function(i, e) { - console.log(e); var item = $('
  • ' + e[1] + '
  • '); ul.append(item); }); @@ -52,7 +49,10 @@ function add_holiday(worker) { $("#holidays").append(li); $("#holiday-dlg").dialog("close"); } + } catch(e) { + $('#holiday-dlg form').html(response); } + }; action(worker, null, 'ajouter', null, null, null, params, on_success); }; @@ -81,11 +81,9 @@ function edit_holiday(worker, holiday) { 'button_close': 'Fermer', 'button_confirm': 'Mettre à jour', 'width': '500px'} - on_success = function(response, status, xhr) { - var content_type = xhr.getResponseHeader('content-type'); - if(content_type.indexOf('html') > -1) { - $('#holiday-dlg form').html(response); - } else { + on_success = function(response) { + try { + $.parseJSON(response); if(!response.err) { $.each(response.content, function(i, e) { $('#holidays #' + holiday + ' li.' + e[0]).html(e[1]); @@ -93,6 +91,11 @@ function edit_holiday(worker, holiday) { $(selector).attr('style', initial_color); $("#holiday-dlg").dialog("close"); } - }} + } catch(e) { + console.log(e); + $('#holiday-dlg form').html(response); + } + + } action(worker, holiday, 'editer', selector, initial_color, '#af7', params, on_success); }; \ No newline at end of file -- 1.8.4.rc2