简体   繁体   English

ASP.NET Core Ajax“完成”,“成功”或“错误”未触发

[英]ASP.NET Core Ajax “done”, “success” or “error” not firing

Here is the thing : I've got an ajax script which sends a "DELETE" request to an API controller, the thing I wanted to remove is indeed deleted from the database and the API returns a 200 OK Response, but no event will fire and I genuinely don't know why. 事情是这样的:我有一个ajax脚本向API控制器发送“ DELETE”请求,我要删除的东西确实已从数据库中删除,API返回了200 OK响应,但是不会触发任何事件我真的不知道为什么。

Here is the script called : 这是称为的脚本:

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $('.delete-project').click(function (e) {
                $.ajax({
                    url: '/api/project/' + id,
                    method: 'DELETE',
                    async: true,
                    dataType: 'text',
                    success: function () {
                        console.log("Success reached");
                    }
                }).done(function () {
                    alert("done");
                    console.log("done");
                }).error(function () {
                    console.log("failed");
                });

                return false;
            });
        });
    </script>
}

I have also tried with "async: false" but unfortunately, it didn't solve my problem. 我也尝试过使用“ async:false”,但是不幸的是,它没有解决我的问题。

(Also, the return false is because I'm clicking on a button that is embedded inside a clickable list, it is to prevent from being redirected after clicking on the button, as if you've clicked anywhere else on the list. I wanted to point that out before I get any question) (此外,返回false是因为我正在单击嵌入在可单击列表中的按钮,这是为了防止在单击按钮后被重定向,就像您单击了列表中的其他位置一样。我想要在我有任何疑问之前指出这一点)

Here is the code executed by the API 这是API执行的代码

// DELETE api/<controller>/5
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(int id)
    {
        var project = await _unitOfWork.Projects.GetById(id);
        if (project == null)
            throw new HttpRequestException();
        else { 
            await _unitOfWork.Projects.Delete(project);
            await _unitOfWork.SaveChangesAsync();

            return Ok("Ok");
        }
    }

Also, there is nothing in the console. 另外,控制台中没有任何内容。 No error message and no message that should appear from the console.log() calls. 没有错误消息,也没有应该从console.log()调用中出现的消息。

This is what is shown in the "network" tab : 这是“网络”标签中显示的内容:

Here are the details about the 200 OK Response in network tab, on Firefox 以下是有关Firefox上“网络”标签中的200 OK响应的详细信息

The jQuery documentation only lists these as valid values for the method param: "GET" , "POST" , or "PUT" . jQuery文档仅将这些列为method参数的有效值: "GET""POST""PUT"

It sounds like you'll want to use XMLHttpRequest directly instead if you want to do a "DELETE" . 听起来,如果要执行"DELETE"则想直接使用XMLHttpRequest It's quite trivial: 这很简单:

$('.delete-project').click(function (e) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                console.log("Succeeded");
            } else {
                console.log("Failed");
            }
        }
    };
    xhr.open("DELETE", "/api/project/" + id);
    xhr.send();

    return false;
});

(You might be able to use the load and error events instead of readystatechange ; I've never tried them with "DELETE" so...) (您也许可以使用loaderror事件而不是readystatechange ;我从没尝试过使用"DELETE"这样的操作,所以...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM