AJAX HTTP方法GET只到服务器一次

本文关键字:一次 服务器 HTTP 方法 GET AJAX | 更新日期: 2023-09-27 18:13:20

当我在action方法中使用断点向服务器发出ajax请求时,它只在这个断点上第一次停止。在点击第二次,第三次等之后,它继续,但永远不会停止在这个断点上。当我把方法从GET改为POST时,它每次都停止。这个行为的原因是什么?

客户端:

$(function () {
    setListAction();
});
function setListAction() {
    $("li.list").on("click", function () {
        alert("active");
        var id = $(this).attr("file-id");
        $.ajax({
            type: "GET",
            url: "TechAcc/ManageFile/" + id,
            beforeSend: function myfunction() {
                $("#loading").css("display", "block");
                $("#fade").css("display", "block");
            },
            success: function (data) {
                var content = $(data).find("div#content");
                $("div#content").html(content.html());
                $("#loading").css("display", "none");
                $("#fade").css("display", "none");
            }
        });
    });
}
服务器端:

[HttpGet]
        public ActionResult ManageFile(int id = 0)
        {
            FileModel model = null;
            if (id != 0)
                model = new FileModel() { File = _repository.GetFileBy(id), GetAllFiles = _repository.GetAllFiles() };
            else if (Session["Model"] != null)
                model = (FileModel)Session["Model"];
            else
                model = new FileModel() { GetAllFiles = _repository.GetAllFiles() };
            return View(model);
        }

AJAX HTTP方法GET只到服务器一次

如果你的div id为"content"有list,它将无法工作。

<div id="content">
if your list is here, it won't work.
...
<li class="list">...</li>
...
</div>

如果您的设计是这样的,您需要在替换HTML响应后绑定click event。例如,

success: function (data) {
                var content = $(data).find("div#content");
                $("div#content").html(content.html());
                //adding code here.
                $("div#content").find("li.list").on("click", function() {
                //same above click code should come here.
                //Note: this newly added code block should not come here in click.
                });
                $("#loading").css("display", "none");
                $("#fade").css("display", "none");
            }
相关文章: