Just noticed a feature in ss-utils called Server Initiated Actions more specifically the “Soft Redirect” on ajax form submissions.
I’m curious on how it works because it doesn’t seem to do a soft redirect. I’m probably using the wrong $.ajax()
is my assumption. Or we need to manually add a check for the header X-Location
? Or is there another way?
// In a Service class:
public HttpResult Any(MyRequest request)
{
// Do Stuff
return HttpResult.SoftRedirect("/home");
}
The clientside ajax is:
$(formId).submit(function(e) {
e.preventDefault();
var $form = $(this);
if (!$form.valid()) {
return false;
}
$(submitId).attr("disabled", "disabled");
$.ajax({
type: $form.attr("method"),
url: $form.attr("action"),
data: $form.serialize(),
dataType: "JSON",
success: function(data) {
// if not a soft-redirect then do:
$form.hide();
document.getElementById("viewAfterSubmit").scrollIntoView();
},
error: function(xhr, err, status) {
$(submitId).removeAttr("disabled");
var r = JSON.parse(xhr.responseText);
var errStatus = r && r.responseStatus;
if ($form) {
$($form).applyErrors(errStatus);
}
return;
}
});
return true;
});