簡體   English   中英

在ASP.NET MVC中,jQuery AJAX PUT方法返回404錯誤(未找到)

[英]JQuery AJAX PUT method return 404 error (not found) in asp.net mvc

這是我的問題,我嘗試使用JQuery AJAX在asp.net mvc中更新我的數據,但是當我使用PUT方法時,它將返回錯誤404,表示找不到控制器,但是如果我使用GET / POST方法,一切工作正常,這是什么問題? 謝謝!

var Test= {
    Update: function (TestId, Test, callback) {

        var errorMsg = "Missing a parameter!";
        if (!TestId) { throw errorMsg; return; }
        if (Test== null) { throw errorMsg; return; }
        if (callback == null) { throw errorMsg; return; }

        var pack = { "test" : Test};

        $.ajax({
            type: "PUT",
            url: "/test/" + TestId+ "/update",
            dataType: "json",
            data: JSON.stringify(pack),
            contentType: 'application/json; charset=utf-8',
            processData: false,
            cache: false
        }).done(function (result) {
            callback(result);
        });

    }
};

絕對不應在MVC URL進行硬編碼

而是使用@Url.Action

url: '@Url.Action("FunctionName", "ControllerName")',

如果您希望發送參數,請使用

url: '@Url.Action("FunctionName", "ControllerName", new { ID = Model.MyID })',

另外,為了您的理智,請使用fail方法。

$.ajax("http://url")
    .done(function() {
    alert("success");
})
    .fail(function() {
    alert("error");
})

如果這不能解決您的問題,請告訴我們您遇到哪種錯誤。

您是否將其設置為.NET的接受項?

[HttpPut]
public JsonResult Update(int id)
{
    return Json("Your Response");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM