代码之家  ›  专栏  ›  技术社区  ›  hsim

javascript ajax发布后未命中MVC Get方法

  •  1
  • hsim  · 技术社区  · 7 年前

    所以我有两种有效的控制器方法:

    [HttpGet]
    public ActionResult EditWork(string work)
    {
        // do the bla bla
        return Json(new {Success = true});
    }
    
    [HttpPost]
    public ActionResult EditWork(WorkObjet work)
    {
        // Do the bla bla!
    
        var listWork = TempData.Peek("myWorks") as List<WorkObjet>;
    
        // Edit the value of the correct work object.
    
        return Json(new {Success = true});
    }
    

    通过Javascript调用它们,如下所示:

    function EditWork(event, anc) {
        event.preventDefault();
    
        var param = anc.attributes["data-id"].nodeValue;
    
        $.ajax({
            type: 'GET',
            url: @Url.Action("EditWork", "Work"),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: { work: param }
        }).done(function (data) {
            if (data.Success === true) {
                // Update the fields
            } else {
                console.log("Error:" + param);
            }
        }).fail(function (xhr) {
            console.log(xhr.responseText);
        });
    }
    
    function SaveEdit(){
        var myWork = GetMyWork() // calls the field via jQuery
    
        $.ajax({
            type: 'POST',
            url: @Url.Action("EditWork", "Work"),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ work: myWork })
        }).done(function (data) {
            if (data.Success === true) {
                // reload the page?
                windows.location.reload();
            } else {
                console.log("Error:" + param);
            }
        }).fail(function (xhr) {
            console.log(xhr.responseText);
        });
    }
    

    它们工作得很好。因此,让我们执行以下操作:

    1. 单击按钮编辑作品
    2. 通过断点命中控制器Get方法EditWork;
    3. 编辑工作,然后保存
    4. 通过断点命中控制器Post方法EditWork,并在列表中更新该工作;
    5. 页面将刷新
    6. 单击按钮再次编辑
    7. GET方法是 未通过断点命中 这个 旧作品 显示在屏幕上。非常奇怪。
    8. 编辑工作,然后保存
    9. POST方法中包含以下值。。。

    第七步怎么可能呢?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Martin D.    7 年前

    将此添加到GET请求中:

    cache: false
    

    默认情况下,jQuery允许浏览器缓存GET Ajax请求的结果。看看 documentation 了解更多信息。