以下是AJAX调用者:
function editItem(id) {
$.ajax({
type: 'GET',
url: '/bookmarkrest/edititem?Id=' + id,
success: function (json) {
//alert('Success.');
window.location.href = json.redirectUrl;
},
error: function () {
alert("No Change.");
}
});
};
以下是调用的方法。 EditLink方法应该带我到另一个页面(指定的页面):
public ActionResult EditItem(int? Id = 0)
{
try
{
Bookmark bookmark = repository.GetBookmark(Id);
if (bookmark is Link)
{
return Json(new { redirectUrl = Url.Action("EditLink", "BookmarkREST", new { Id = Id }) });
}
return RedirectToAction("Index", "Home");
}
catch (Exception)
{
return RedirectToAction("Index", "Home");
}
}
[HttpGet]
public ActionResult EditLink(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Link link = repository.GetLink(id);
if (link == null)
{
return HttpNotFound();
}
return View(new LinkViewModel { name = link.Name, uri = link.URI });
}
我也在Chrome开发者工具的网络请求响应标签中收到了正确的回复:
我不确定为什么我会被保留在同一页面上,有人可以帮忙吗?
Ajax请求必须是POST和数据类型属性:
function editItem(id) {
$.ajax({
type: 'POST',
datatype: 'JSON',
url: '/bookmarkrest/edititem?Id=' + id,
success: function (json) {
//alert('Success.');
window.location.href = json.redirectUrl;
},
error: function (json) {
alert(json.message);
}
});
};