我想发送一个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>

最佳答案

更改自:

<form name="htmlform">

至:
<form id="htmlform">

来自:
var queryString = $('#htmlform')

至:
var queryString = $('#htmlform').serialize();

关于javascript - 使用jQuery创建格式不正确的查询字符串不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11020769/

10-11 15:04