Fix jQuery resturn status.

This commit is contained in:
Paulo Gustavo Veiga 2014-02-01 22:48:01 -03:00
parent 94b043ecf5
commit d6656dffc0

View File

@ -98,6 +98,53 @@ jQuery.fn.dialogForm = function (options) {
$('#' + containerId + ' input').each(function (index, elem) { $('#' + containerId + ' input').each(function (index, elem) {
formData[elem.name] = elem.value; formData[elem.name] = elem.value;
}); });
// Success actions ...
var onSuccess = function (jqXHR, textStatus,data) {
if (options.redirect) {
var resourceId = jqXHR.getResponseHeader("ResourceId");
var redirectUrl = options.redirect;
redirectUrl = redirectUrl.replace("{header.resourceId}", resourceId);
// Hack: IE ignore the base href tag ...
var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("c/maps/"));
window.open(baseUrl + redirectUrl, '_self');
} else if (options.postUpdate) {
options.postUpdate(formData);
}
dialogElem.modal('hide');
};
// On error message
var onFailure = function(jqXHR,textStatus, data){
var errors = JSON.parse(jqXHR.responseText);
// Mark fields with errors ...
var fieldErrors = errors.fieldErrors;
if (fieldErrors) {
for (var fieldName in fieldErrors) {
// Mark the field with errors ...
var message = fieldErrors[fieldName];
var inputField = $("#" + containerId + " input[name='" + fieldName + "']");
$("#" + containerId).find(".errorMessage").text(message).addClass("alert alert-error");
inputField.parent().addClass('error');
}
}
var acceptBtn = $('#' + containerId + ' .btn-accept');
acceptBtn.button('reset');
};
var onError = function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
console.log(jqXHR);
dialogElem.modal('hide');
$('#messagesPanel div div').text(errorThrown);
$('#messagesPanel').show()
var acceptBtn = $('#' + containerId + ' .btn-accept');
acceptBtn.button('reset');
};
$(acceptBtn).button('loading'); $(acceptBtn).button('loading');
var dialogElem = this; var dialogElem = this;
jQuery.ajax(url, { jQuery.ajax(url, {
@ -106,47 +153,14 @@ jQuery.fn.dialogForm = function (options) {
data:JSON.stringify(formData), data:JSON.stringify(formData),
type:options.type ? options.type : 'POST', type:options.type ? options.type : 'POST',
contentType:"application/json; charset=utf-8", contentType:"application/json; charset=utf-8",
success:function (data, textStatus, jqXHR) { statusCode:{
if (options.redirect) { 200: onSuccess,
var resourceId = jqXHR.getResponseHeader("ResourceId"); 201: onSuccess,
var redirectUrl = options.redirect; 204: onSuccess,
redirectUrl = redirectUrl.replace("{header.resourceId}", resourceId); 400: onFailure,
444: onError,
// Hack: IE ignore the base href tag ... 500: onError,
var baseUrl = window.location.href.substring(0, window.location.href.lastIndexOf("c/maps/")); 501: onError
window.open(baseUrl + redirectUrl, '_self');
} else if (options.postUpdate) {
options.postUpdate(formData);
}
dialogElem.modal('hide');
},
error:function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 400) {
var errors = JSON.parse(jqXHR.responseText);
// Mark fields with errors ...
var fieldErrors = errors.fieldErrors;
if (fieldErrors) {
for (var fieldName in fieldErrors) {
// Mark the field with errors ...
var message = fieldErrors[fieldName];
var inputField = $("#" + containerId + " input[name='" + fieldName + "']");
$("#" + containerId).find(".errorMessage").text(message).addClass("alert alert-error");
inputField.parent().addClass('error');
}
}
} else {
console.log(errorThrown);
console.log(jqXHR);
dialogElem.modal('hide');
$('#messagesPanel div div').text(errorThrown);
$('#messagesPanel').show()
}
var acceptBtn = $('#' + containerId + ' .btn-accept');
acceptBtn.button('reset');
} }
}); });
}.bind(this)); }.bind(this));