Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发送GET方法而不是DELETE方法_Java_Html_Ajax_Spring_Thymeleaf - Fatal编程技术网

Java 发送GET方法而不是DELETE方法

Java 发送GET方法而不是DELETE方法,java,html,ajax,spring,thymeleaf,Java,Html,Ajax,Spring,Thymeleaf,我正在尝试编写应用程序的前端,但遇到了一个问题。我一直在尝试使用AJAX实现一个DELETE方法,但是根据Spring的说法,当我运行代码时会发送一个GET HTML代码: <tr th:each="attraction : ${attractions}" th:object="${attraction}"> <td th:text="*{name}"></td> <td th:text="*{latitude}"><

我正在尝试编写应用程序的前端,但遇到了一个问题。我一直在尝试使用AJAX实现一个DELETE方法,但是根据Spring的说法,当我运行代码时会发送一个GET

HTML代码:

    <tr th:each="attraction : ${attractions}" th:object="${attraction}">
    <td th:text="*{name}"></td>
    <td th:text="*{latitude}"></td>
    <td th:text="*{city}"></td>
    <td><a th:href="|/edit/*{id}|">EDIT</a></td>
    <script>
        function sendDelete(event) {
            xhttp.preventDefault();
            xhttp.open("DELETE", this.href);
            xhttp.send();
        }
    </script>
    <td><a th:href="|/delete/*{id}|" onclick="sendDelete(event);">DELETE</a></td>
</tr>

我怎样才能解决这个问题?提前谢谢你。

有人帮忙,我就可以解决这个问题了。基本问题是
标记只能处理GET方法

相反,我将代码的这一部分用HTML进行了如下分类:

    <td>
        <form th:method="DELETE" th:action="|/delete/*{id}|">
            <input type="submit" value="Send">
        </form>
    </td>


现在它工作得很好。

您花了很长时间才解决了这个问题

链接标记可以发送任何您想要的http方法,只要您使用JavaScript处理它并调用preventDefault

但是您必须在传递到click处理程序的事件上执行此操作,而不是在xhttp pbject上。因此,在事件处理程序上,您应该执行以下操作

event.preventDefault()
而不是:

xhttp.preventDefault()

您的表单hack不是惯用的。它会吓坏下一个处理代码的人

根据spring,发送get是什么意思?你是说spring代码中的delete方法永远不会被调用吗?可能的重复请参见以下答案:这与你的问题无关,但你应该在此处使用“@{…}”和th:href。当设置不同于“/”.@RobertMoskal的上下文根时,您会遇到问题。我只是想指出,我得到了一个白标错误,但是的,它不是在Spring之前完成的……我认为您已经走了很长的路。
xhttp.preventDefault()