javascript 警告:不支持请求方法“GET”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16097202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WARNING: Request method 'GET' not supported
提问by
In my Spring Application i'm doing like this..
在我的 Spring 应用程序中,我正在这样做..
My Jsp form click hyper link form will be sent..
我的 Jsp 表单点击超链接表单将被发送..
<form:form action="user" modelAttribute="NAME">
<a href="href_form1" onclick="onClick_Save()" class="save">Save</a>
</fomr:form>
<script>
function onClick_Save() {
$("#NAME").attr("action", $('.save').attr("href"));
$("#NAME").submit();
});
</script>
In my Controller class i wrote method.
在我的 Controller 类中,我编写了方法。
@RequestMapping(value = "href_form1", method = RequestMethod.POST)
public @ResponseBody String href_form1(UserForm userForm,Model model)throws Exception {
//Database code here.
model.addAttribute("NAME", userForm);
return "User Updated";
}
EDIT1:
编辑1:
WARNING: Request method 'GET' not supported
and i'm getting error page.
我收到错误页面。
HTTP Status 405 - Request method 'GET' not supported
EDIT:after modify the code like below
编辑:修改后的代码如下
<script>
$(function() {
$('.save').on("click",function(e) {
e.preventDefault();
$("#NAME").attr("action", $(this).attr("href")).attr("method","POST");
$("#NAME").submit();
});
});
</script>
But my form will not reach the my controller method.
但是我的表单不会到达我的控制器方法。
Is there anything wrong in my code? I've also tried method="POST"
in the form tag.
我的代码有什么问题吗?我也在method="POST"
表单标签中尝试过。
采纳答案by mplungjan
Here is the reason you are getting the error
这是您收到错误的原因
The link's href is calling the controller when clicked because you do not cancel the default action of the link
单击链接时,链接的 href 正在调用控制器,因为您没有取消链接的默认操作
So the code does the following
所以代码执行以下操作
- attempts to set the action of a form to the href of the link
- attempts to submit said form using POST - this is what I understand you want to achieve.
- regardless of whether or not this succeeds, the browser immediately follows the href of the link which will try to call the controller with a GET as a link will do if the onclick does not stop that action. Even if the code succeeded in POSTing the form, that request will be killed by the new request which ends up doing a GET
- 尝试将表单的操作设置为链接的 href
- 尝试使用 POST 提交所述表单 - 这就是我理解您想要实现的。
- 无论这是否成功,浏览器都会立即跟随链接的 href 链接,如果 onclick 没有停止该操作,它将尝试使用 GET 调用控制器作为链接。即使代码成功发布表单,该请求也会被新请求杀死,新请求最终会执行 GET
In my opinion you have the following possibilities - some of them are supposedly better practice than others.
在我看来,您有以下可能性 - 其中一些可能比其他人更好。
Non-javascript
非 JavaScript
- Have a parameter that holds the identification of what you want to save - and a form per parameter
- change your spring method to
method = { RequestMethod.GET, RequestMethod.POST}
ormethod = RequestMethod.GET
- the GET will allow you to get rid of the form and just have the following link (after updating the spring code of course)
- 有一个参数来保存您要保存的内容的标识 - 以及每个参数的表单
- 将您的 spring 方法更改为
method = { RequestMethod.GET, RequestMethod.POST}
或method = RequestMethod.GET
- GET 将允许您摆脱表单并只有以下链接(当然是在更新 spring 代码之后)
<a href="href_link?user=user1">Save User1</a>
if you so wish
如果你愿意
JavaScript assisted
JavaScript 辅助
Have a parameter in the form that holds the identification of what you want to save, set THAT by JavaScript and perform a MANDATORYreturn false
or preventDefault
to cancel the link:
在表单中有一个参数,用于保存要保存的内容的标识,通过 JavaScript 设置 THAT 并执行MANDATORYreturn false
或preventDefault
取消链接:
<!-- this is an example of the rendered form -->
<form action="href_form1" id="NAME" method="POST">
<input type="hidden" name="saveItem" id="saveItem" value="" />
</form>
<a href="#" class="saveLink" data-save="save item 1">Save item 1</a>
<a href="#" class="saveLink" data-save="save item 2">Save item 2</a>
<a href="#" class="saveLink" data-save="save item 3">Save item 3</a>
Please note I do not have a link in the href to avoid requesting something
请注意,我在 href 中没有链接以避免请求某些内容
The jQuery to go with this process:
与此过程一起使用的 jQuery:
$(function() {
// assuming form ID="NAME"
$(".saveLink").on("click",function(e) {
e.preventDefault(); // do not follow link
$("#saveItem").val($(this).data("save")); // set the hidden field
$("#NAME").submit();
});
});
Here is a perhaps more interesting way for you, assuming your spring controllers are hooked up to handle your requests:
假设您的 spring 控制器已连接以处理您的请求,这对您来说可能是一种更有趣的方式:
$(function() {
$('.save').on("click",function(e) { // passing event
e.preventDefault(); // jQuery will normalise it for all modern browsers
// post the href of the link to the server and return the result in a
// container with ID result
$.post($(this).attr("href"), function(data) { // or $.get if you want
$('#result').html(data);
});
});
回答by SimonDever
You need to specify the method
in the form
tag. And yes, your hyperlink is ignoring the form. So set the action
attribute on the form to href_form1
and change your hyperlink to a submit input or (and I wouldn't really recommend this) change your hyperlink to do a JavaScript submission. At the moment, you have onclick
andhref
setup in the link. Below is best practise.
您需要method
在form
标签中指定。是的,您的超链接忽略了表单。因此action
,将表单上的属性设置为href_form1
并将您的超链接更改为提交输入或(我真的不建议这样做)更改您的超链接以执行 JavaScript 提交。此刻,你必须onclick
和href
链路设置。下面是最佳实践。
<form:form action="href_form1" modelAttribute="NAME" method="POST">
<input type="submit" value="Save" />
Also your jQuery selector $("#NAME")
isn't finding anything because you haven't set the id
attribute on the form you want to get.
此外,您的 jQuery 选择器$("#NAME")
没有找到任何内容,因为您尚未id
在想要获取的表单上设置属性。
回答by Akshay
Please try something like this
请尝试这样的事情
<form:form action="href_form1" modelAttribute="NAME" method="POST">
<input type="submit"/>
</form:form>
@RequestMapping(value = "href_form1", method = RequestMethod.POST)
public @ResponseBody String href_form1(UserForm userForm,Model model)throws Exception {
//Database code here.
model.addAttribute("NAME", userForm);
return "User Updated";
}
Basically a very simple form submit. Once you get this working, you know that your form and controller are properly setup. After this if you want to use a anchor tag to submit the form, go back to earlier suggestions, preventDefault
and then form submit.
基本上是一个非常简单的表单提交。一旦你开始工作,你就知道你的表单和控制器已经正确设置了。在此之后,如果您想使用锚标记提交表单,请返回之前的建议,preventDefault
然后提交表单。
Hope this helps.
希望这可以帮助。
回答by Bhushan Bhangale
Default http method for form submission is GET. Since you want POST please specify that and it will work liek method="POST"
表单提交的默认 http 方法是 GET。既然你想要 POST 请指定它,它会起作用method="POST"
Also I do not see the url mapping correctly in the code. Could you please confirm if its correct.
我也没有在代码中正确地看到 url 映射。请您确认一下是否正确。