表单使用get提交,问号传值问题

最近换工作,从以前做中间件服务器换到了移动互联网的app服务端开发,也就又重新使用一些开源框架和JavaScript等技术,以前工作都是使用原始JavaScript,自己封装函数,自己写代码来实现功能,现在项目中在使用jQuery框架,突然发现jQuery使用起来好方便啊。
今天在写代码过程中发现项目的一个bug,项目中使用jQuery进行表单提交,使用get方式提交,想在action的url中通过问好?来携带get参数,代码入下:
var url = "${pageContext.request.contextPath}/Project/path?"+"tel="+""+"&startTime="+startTime+"&endTime="+endTime;
$("form").attr("action",url);
$("form").attr("method","get");
$("form").submit();

但是在后台,通过request.getParameter(“startTime”)来取值时,却一直取不到值,上网查看发现:HTML规定,通过GET方法提交表单时,action地址里的query串会被丢弃。原文如下:
HTML 4.0.1(http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4
If the method is “get” and the action is an HTTP URI, the user agent
takes the value of action, appends a `?’ to it, then appends the form
data set, encoded using the “application/x-www-form-urlencoded”
content type.
HTML 5(http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#form-submission-algorithm
Mutate action URL
Let query be the result of encoding the form data
set using the application/x-www-form-urlencoded encoding algorithm,
interpreted as a US-ASCII string.
Let destination be a new URL that is equal to the action except that
its component is replaced by query (adding a U+003F QUESTION
MARK character (?) if appropriate).
因此如果要实现该功能,则不能使用问号在url中携带参数,可以在表单中使用隐藏域,即hidden来隐藏,再使用jQuery来赋值,后提交,使用post提交方式即可。或者也可以使用js,在获取form后使用append来追加隐藏域,这样也可以把值传过去。当然也许还有更好的方法,本人试过第一种,没有问题。
在此可以看出,其实很多细节的东西会被我们忽略掉,尤其是在做js相关的东西,零碎的东西太多,一定要注意细节,在这些方面,细节决定成败。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章