Revert "Revert "Improve ajax errors [MAILPOET-929]""

This reverts commit 81c3e2facf.
This commit is contained in:
Pavel Dohnal
2017-06-26 13:51:53 +01:00
parent 836b7179e9
commit 2285c08c01
2 changed files with 88 additions and 74 deletions

View File

@@ -1,5 +1,19 @@
function requestFailed(errorMessage, xhr) {
if (xhr.responseJSON) {
return xhr.responseJSON;
}
var message = errorMessage.replace("%d", xhr.status);
return {
errors: [
{
message: message
}
]
}
}
define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) { define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) {
'use strict';
MailPoet.Ajax = { MailPoet.Ajax = {
version: 0.5, version: 0.5,
options: {}, options: {},
@@ -44,7 +58,6 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery,
// set request params // set request params
var params = this.getParams(); var params = this.getParams();
var deferred = jQuery.Deferred();
// remove null values from the data object // remove null values from the data object
if (_.isObject(params.data)) { if (_.isObject(params.data)) {
@@ -54,16 +67,14 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery,
} }
// ajax request // ajax request
deferred = jQuery.post( var deferred = jQuery.post(
this.options.url, this.options.url,
params, params,
null, null,
'json' 'json'
).then(function(data) { ).then(function(data) {
return data; return data;
}, function(xhr) { }, _.partial(requestFailed, MailPoet.I18n.t('ajaxFailedErrorMessage')));
return xhr.responseJSON;
});
// clear options // clear options
this.options = {}; this.options = {};

View File

@@ -55,6 +55,9 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
'mailpoet.js' 'mailpoet.js'
)%> )%>
<%= localize({
'ajaxFailedErrorMessage': __('An error has happened while performing a request, the server has responded with response code %d'),
}) %>
<% block translations %><% endblock %> <% block translations %><% endblock %>
<% block after_translations %><% endblock %> <% block after_translations %><% endblock %>