ajax:发送表单,无法接收参数?

时间:2014-06-16 04:11:20

标签: ajax forms

我尝试使用ajax发送表单。我可以成功发送信息。我可以收到这些信息。但是,我无法从请求中获取参数。我不知道原因。

我尝试IE,FIREFOX,Chrome,没有人可以发送表单。

这是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Expires" content="0">
<meta http-equiv="kiben" content="no-cache">
<title>Login Page</title>
<%String account =(String) session.getAttribute("fail"); %>
</script>
<script language="javascript"  type="text/JavaScript"  src="jquery-1.3.2.min.js">
</script>
<script language="javascript"  type="text/JavaScript">
window.onload =  function fillChecklist(){
    var error ="${sessionScope.fail}";;
    if(null != error&& error!=""){
        alert(error);
    }
}

    function doFind() {
        if(document.getElementById("username").value.length>0&&document.getElementById("password").value.length>0){
        $.ajax({
            cache : false,
            type : "POST",
            url : "login", 
            data : $("#ajaxFrm").serialize(), 
            async : false,
            error : function(request) {
                alert(“error");
            },
            success : function(data) {
                //$("#ajaxDiv").html(data);
                alert(data);
            }
        });
        }
    }
</script>
</head>
<body>
        <form id="ajaxFrm" >
        UserName<input type="text" id="username" ><br />
        Password &nbsp<input type="password" id="password">
        </form>
        <input type="checkbox" name="remember" id="remember">Remeber Username</input><br />
        <input value="Submit" type="button" onClick="doFind()"/> 
        <input value="Delete" type="button" onClick="DelCookie()"/>
        <div id="ajaxDiv"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

请更改输入类型=&#34;按钮&#34;输入类型=&#34;提交&#34;首先,然后使用ajax.e.g提交

<html>
<head>
<script type="text/javascript"  src="jquery-1.8.3.js"></script>
<script type="text/javascript">
    $("form#ajaxFrm").submit(function() {
        var mydata = $("form#ajaxFrm").serialize();
        console.log(mydata); //check data before send to server
        $.ajax({
            type: "POST",
            url: "myUrlToPostData.php",
            data: mydata,
            success: function(response, textStatus, xhr) {
                alert("success");
            },
            error: function(xhr, textStatus, errorThrown) {
                alert("error");
            }
        });
        return false;
    });
</script>
</head>
<body>
    <form id="ajaxFrm" method="post">
        UserName<input type="text" id="username"  name="username"/><br />
        Password <input type="password" id="password" name="password"/>
        <input value="submit" type="submit" name="submit"/>
    </form>
 </body>
</html>