没用Ajax( )请求代码前,我们是用
< form action=“http://127.0.0.1/01.php” method=“post”>来向服务器端发送请求的,那如果没有action属性, method属性和< form >标签,该如何向服务器端发送请求呢?
没有action属性, method属性和< form >标签,如下图,那就要用Ajax( )来向服务器端发送请求了
表单中,键是自定义的:值是value属性的值
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
h1{margin: 0 auto;text-align: center;}
#zxw{
width: 500px;margin: 50px auto;
}
input{
width: 400px;height: 40px;
}
</style>
<script src="../js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
//给按钮加单击事件
$("#sbt").click(function(){
//$.ajax()方法接收的是一个对象{ },对象是很多属性和方法的集合
$.ajax({
type:"POST", //ajax请求使用POST方法或get方法(POST方法最常用)
url :"服务器地址", //请求的服务器地址
data:{ //发送到服务器的数据,JSON 数据类型
"键1":"值1",
"键2":"值2",
},
dataType:"json", //指定:从服务器返回 JSON 数据类型
success:function(参数){
//请求成功要执行的代码
//参数:从服务器返回的数据
},
error:function(){
//请求失败要执行的代码
}
});
});
})
</script>
</head>
<body>
<h1>登陆</h1>
<div id="zxw">
姓名:<input type="text" name="uname" id="uname" value="" />
<br /><br />
密码:<input type="password" name="password" id="password" value="" />
<br /><br />
<input type="submit" id="sbt" value="提交按钮"/>
</div>
</body>
</html>