Ajax HttpPost在某些浏览器中导致/Unfine重定向的原因
背景故事: 我最近遇到了一个问题,当我在MVC页面上单击我的删除按钮删除一条记录时,它会删除该记录,但会立即重定向到Ajax HttpPost在某些浏览器中导致/Unfine重定向的原因,ajax,asp.net-mvc-4,redirect,Ajax,Asp.net Mvc 4,Redirect,背景故事: 我最近遇到了一个问题,当我在MVC页面上单击我的删除按钮删除一条记录时,它会删除该记录,但会立即重定向到localhost/undefined,而不调用任何重定向。我在点击按钮时使用jqueryajax调用,删除操作返回一个Json(new{Success=true})。不过,这在FireFox和IE(32位和64位版本)中运行得非常好;它导致在Chrome、Opera和Safari浏览器中重定向到localhost/undefined 我通过删除控制器上删除操作的[HttpPost
localhost/undefined
,而不调用任何重定向。我在点击按钮时使用jqueryajax调用,删除操作返回一个Json(new{Success=true})代码>。不过,这在FireFox和IE(32位和64位版本)中运行得非常好;它导致在Chrome、Opera和Safari浏览器中重定向到localhost/undefined
我通过删除控制器上删除操作的[HttpPost]
属性解决了这个问题
代码:(HTML/Razor)
代码:操作(解决问题之前)
啊。我有一个我忘了的小宝宝
/* Entity Selection */
function onImageClick(e) {
var image = $(this);
var link = $(image).attr("href");
window.location.href = link;
}
// Enabled onClick events.
$(document).on("click", "tbody tr td img", onImageClick); // Solved the problem by adding ".selectEntity" to the selector.
我想这可以解释我为什么会有问题。咳嗽。很抱歉。感谢您提供的所有帮助。您能显示正在进行重定向的页面的标题吗?这可能会有很大帮助。试着在每个变量之后执行console.log(),看看会转储什么,然后您可能会快速解决问题。是的,您应该使用POST。检查网络请求/响应头以提供更多线索。可以使用e.preventDefault()
而不是返回false代码>在mydeleteEntity
函数的末尾导致它?一位同事提出了这一点。我会把代码放回原样,检查标题,看看我同事的担心是否正确。让我们来看看。
// 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);
/// <summary>
/// POST: Deletes the specified entity from the database.
/// </summary>
[HttpPost]
public ActionResult Delete(Int32? EntityID )
{
// Delete the estimate given its ID.
if ((EntityID ?? 0) > 0)
{
Db.DeleteEntity(EntityID ?? 0);
if (EntityID == this.ActiveSelection.EstimateID)
{
this.ActiveSelection = null;
}
}
return Json(new { Success = true });
}
Remote Address:127.0.0.1:59635
Request URL:http://localhost:59635/Estimate/Delete
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:11
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ASP.NET_SessionId=xwxxnwy3ke2zvtztnnsngz3a
Host:localhost:59635
Origin:http://localhost:59635
Referer:http://localhost:59635/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
EntityID:25
Response Headersview source
Cache-Control:private
Connection:Close
Content-Length:16
Content-Type:application/json; charset=utf-8
Date:Mon, 09 Jun 2014 17:53:37 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:4.0
/* Entity Selection */
function onImageClick(e) {
var image = $(this);
var link = $(image).attr("href");
window.location.href = link;
}
// Enabled onClick events.
$(document).on("click", "tbody tr td img", onImageClick); // Solved the problem by adding ".selectEntity" to the selector.