<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<button id="buts">ajax通用</button>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
//以下为通用的方式使用ajax
$('#buts').click(function(){
var num = 10;
//$.ajax({})
$.ajax({
//参数1: url 设置访问的Php文件
url:'06_ajax.php',
//参数2:data:用于传值
data:'id=1000',
//参数3:用于设置连接的方式
type:'GET',
//参数4:success成功后执行什么
success:function(data){
num = 100;
console.log(data);
},
//参数5:error 失败后执行什么
error:function(){
alert('ajax错误');
},
//timout 用于设置最大等待时间,无设置默认不填
//当timout设置的时间内ajax未访问成功请求数据的话,则判定为失败
timout:3000,
//async 是否异步,如果异步为true,同步为false
async:false
});
alert(num);
})
</script>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号