ajax请求get post用法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45747808/article/details/102727738
<form action="#">

账号:<input type-"text" name= "name" id= "name" /x<br/>
密码:<input type= "text" name="pwd" id= "pwd" /><br/>
<input type="button" value="登陆" />
</form>
<div id="show"></div>
<script>
$(function(){
$(" :button"). on("click", function(){
$.ajax({
type: "post" ,
url: "test",
data:{
name:$("#name").val(),
pwd:$("#pwd").val()
},
dataType: "text" ,
success: function(data){
if(data == "ok"){
window. location.href="show.jsp";
}else{
$("#show"). text("登彖失敗!");

}
}
});
});


 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

      doPost( request, response);
}
      public void doPost (HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
      request. setCharacterEncoding( "utf-8");
      response. setContentType("text/html;charset-utf-8");
      PrintWriter out = response. getWriter();
      string name="张三" 

      string pwd = "123456admin";

      string ajaxName = request. getParameter("name");
      string ajaxPwd =request . getParameter(" pwd");
      System. out .println(ajaxName+": "+ajaxPwd);

      if(name.equals (ajaxName)&&pwd.equals(ajaxPwd))(

      out. print("ok");
      }else{

      out . print("error");
      }

      out.flush(); 
      out.close();
      }

    

猜你喜欢

转载自blog.csdn.net/weixin_45747808/article/details/102727738