在对应该返回JSON中的重定向URL的控制器方法的Ajax GET请求之后,我收到500错误而没有达到AJAX成功

问题描述 投票:0回答:1

以下是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 });

    }

enter image description here

我也在Chrome开发者工具的网络请求响应标签中收到了正确的回复:enter image description here

如果我输入URL,我会访问包含数据的正确页面:enter image description here

我不确定为什么我会被保留在同一页面上,有人可以帮忙吗?

asp.net ajax asp.net-mvc
1个回答
0
投票

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);
                }
            });
        };
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.