I'm working on an internal MVC site.
Issue:
Suddenly the site has started redirecting to localhost/undefined
in select browsers (Chrome, Opera, and Safari) before/after a delete POST. This does not happen in Firefox or IE.
Steps Taken:
I've stepped through the following javascript code numerous times on Chrome(not working) and IE(working). There appear to be no difference other than Chrome finishing with a random redirect that is not called upon in the code.
Jquery AJAX Call below:
// Delete button clicked
function deleteEntity(e) {
e.preventDefault();
var name = $(this).attr("name");
var index = $(".deleteEntity[name=" + name + "]").index($(this));
var entityId = ko.utils.unwrapObservable(viewModel.model.peek()[index].ID);
var data = { EntityID: entityId };
viewModel.model.remove(viewModel.model.peek()[index]);
if (entityId > 0) {
$.ajax({
url: "/" + name + "/Delete",
type: "POST",
data: data,
dataType: 'json',
async: true,
success: function (data, textStatus, xhr) {
alert(data);
},
error: function (xhr, textStatus, errorThrown) {
// Decide later.
}
});
}
}
$(document).on("click", ".deleteEntity", deleteEntity);
Debugging Steps:
Starting at a break at the line of the ajax call. -> if (entityId > 0) {
.
success
and error
functions, it ends up not hitting either of the breakpoints and redirecting to localhost/undefined
without contacting the server.localhost/undefined
.Question: Does anyone know the origin of this problem? I've googled around and found a very similar issue relating to plugins so I tried disabling all of the (a whole 2) and it did not change anything.
p.s. if any other code snippets will be helpful just comment and I'll add them. I've checked my actions over at least 20 times. The Delete
is an [HttpPost]
but so is my Update
and it's working fine in the same exact fashion.
I solved the problem by removing the [HttpPost]
attribute from my Delete action in my controller. (I had to add the Json return to allow get but that was just because it was no longer a post request).
I am clue-less as to why it was causing such an error and will be asking a separate question in regards to that. If an answer is found I will link it here in this post.