简体   繁体   English

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

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

here is my problem, I try to use JQuery AJAX update my data in asp.net mvc, but when I use PUT method, it will return a error said 404, means it can not find my controller, but if I use GET/POST method, everything is working fine, so what's the problem? 这是我的问题,我尝试使用JQuery AJAX在asp.net mvc中更新我的数据,但是当我使用PUT方法时,它将返回错误404,表示找不到控制器,但是如果我使用GET / POST方法,一切工作正常,这是什么问题? thanks! 谢谢!

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);
        });

    }
};

You should never hard-code URLs in MVC . 绝对不应在MVC URL进行硬编码

Instead use @Url.Action . 而是使用@Url.Action

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

If you wish to send parameters use 如果您希望发送参数,请使用

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

Also, for the sake of your sanity, use the fail method. 另外,为了您的理智,请使用fail方法。

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

If this doesn't fix your problem tell us what kind of error(s) are you getting. 如果这不能解决您的问题,请告诉我们您遇到哪种错误。

Did you set it to be a accept put on the .NET side? 您是否将其设置为.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