Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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
Javascript 使用jQuery-isn'创建一个形式外的querystring;行不通_Javascript_Jquery_Query String - Fatal编程技术网

Javascript 使用jQuery-isn'创建一个形式外的querystring;行不通

Javascript 使用jQuery-isn'创建一个形式外的querystring;行不通,javascript,jquery,query-string,Javascript,Jquery,Query String,我想发送一个ajax请求,但我试图构建的查询字符串是空的 <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>This is a project to show how to use RESTful</title> </head> <body> <script type="text/javascript"&g

我想发送一个ajax请求,但我试图构建的查询字符串是空的

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is a project to show how to use RESTful</title>
</head>
<body>
<script type="text/javascript">var contexPath = "<%=request.getContextPath()%>";</script>
<script src="<%=request.getContextPath()%>/js/jquery.js"></script>


<script type="text/javascript">
function doAjaxPost() {
    var queryString = $('#htmlform') // empty

    alert("doAjaxPost Called:" + queryString + ":");

    $.ajax({
        ...
        ...
    });
}​    </script>

<H1>Add Employee</H1>

<p>
<form name="htmlform">
<table border=1>
    <thead><tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr></thead>

    <tr>
        <td><input  type="text" name="ID" maxlength="5" size="3"></td>
        <td><input  type="text" name="Name" maxlength="10" size="10"></td>
        <td><input  type="text" name="Email" maxlength="10" size="10"></td>
    </tr>

</table>
<input type="button" value="Save Employee" onclick="doAjaxPost();" />
<p>
<p>
</form>
[<a href="http://localhost:8080/RESTful/service/employees">List all Employees</a> | <a href="add.jsp">Employee Form Test</a>]
</body>
</html>

这是一个展示如何使用RESTful的项目
var=”;
函数doAjaxPost(){
var queryString=$('#htmlform')//空
警报(“doAjaxPost调用:“+queryString+”:”);
$.ajax({
...
...
});
}​    
添加员工

身份证件
名称
电子邮件


[ | ]

您正在使用jQuery选择一个HTML对象,但没有从中获得任何东西(当您将queryString附加到url字符串时,它仍然是一个jQuery对象)

试试下面的方法

var queryString = $('#htmlform').serialize();
这将导致表单被序列化为字符串,此时追加表单应该可以正常工作。

更改为:

<form name="htmlform">
致:


您需要在表单上说
id=“htmlform”
,才能使
$(“#htmlform”)
正常工作。

您会遇到哪些错误
var queryString=$(“#htmlform”)
应以分号(
)结尾@ManseUK<代码>是可选的,请阅读下面我的答案。@gdoron ok谢谢(re
)。。有
名称的好地方
/
id
<form id="htmlform">
var queryString = $('#htmlform')
var queryString = $('#htmlform').serialize();