【问题标题】:Internal server error 500 while ajax callajax 调用时出现内部服务器错误 500
【发布时间】:2014-05-06 20:31:17
【问题描述】:

我正在尝试调用此方法:

// POST: /Manufacturer/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
    Manufacturer manufacturer = unitOfWork.manufacturerRepository.GetByID(id);
    unitOfWork.manufacturerRepository.Delete(manufacturer);
    unitOfWork.Save();
    return Json(new { ok = true, newurl = Url.Action("Index", "Manufacturer") });
}

使用这个 ajax:

$.ajax({
            url: "/Manufacturer/Delete/" + $('#Id').val(),
            type: "POST",
            contentType: "application/json; charset=utf-8",
            headers: {
                'RequestVerificationToken': '@TokenHeaderValue()'
            },
            data: { id: $('#Id').val() },
            error: function (data) {
                alert("error" + data);
            },
            success: function (data) {
                if (data.ok)
                {
                    $("#Modal").modal('hide');
                }
                else {
                    $('.modal-body').html(data);
                }
            }
        })

我签入了提琴手,我可以看到 ID 被正确传递。我将断点放在方法的开头,但没有被击中。之前通话崩溃。 我应该怎么做才能找到导致此崩溃的原因?

来自提琴手的数据:

POST http://localhost:1809/Manufacturer/Delete/34 HTTP/1.1
Host: localhost:1809
Connection: keep-alive
Content-Length: 5
Accept: */*
RequestVerificationToken: TQY9NrchHlFT6IBaTv1R4daiwGoRH0yv3gHJwpatGj
Origin: http://localhost:1809
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:1809/Admin/Index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: __RequestVerificationToken=r5YwV-v

id=34

【问题讨论】:

  • HTTP 请求和响应是什么样的?
  • 如果您使用 debug=true 进行调试,请编辑并写入 HTTP 500 详细错误。
  • 尝试数据:JSON.stringify({ id: $('#Id').val() }),

标签: jquery ajax asp.net-mvc


【解决方案1】:

去掉POSTUrl中的id。Url中的Id用于GET请求。但是如果你想使用POST,在ajax调用的数据部分使用Json.stringify()。

$.ajax({
        url: "/Manufacturer/Delete",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        headers: {
            'RequestVerificationToken': '@TokenHeaderValue()'
        },
        data: JSON.stringify({ id: $('#Id').val() }),
        error: function (data) {
            alert("error" + data);
        },
        success: function (data) {
            if (data.ok)
            {
                $("#Modal").modal('hide');
            }
            else {
                $('.modal-body').html(data);
            }
        }
    })

【讨论】:

    【解决方案2】:

    尝试从帖子 URL 中删除 id。这样它将它传递给正确的方法,并且 id 可以绑定到发布的数据“id”

    $.ajax({
                url: "/Manufacturer/Delete/"
                .......
    

    当传递 id 时,它应该是一个 JSON 字符串,这里我们可以使用 JSON.stringify(value[, replacer [, space]]).

    数据:JSON.stringify({ id: $('#Id').val() })

    如果您愿意,您也可以使用 XML 并将其解析为字符串:

    (new XMLSerializer()).serializeToString(xml);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-06
      • 2017-04-16
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多