jQuery封装的ajax


$.get(url  [,data]  [,fn回调函数]   [, dataType]);

  • data:给服务器传递的数据,请求字符串 、json对象 都可以设置

  • fn:回调函数,ajax请求完成后调用该函数,可以在此函数完成ajax的后续处理

  • dataType:服务器返回数据类型,html、text、xml、json

注:该ajax是异步的get方式请求


$.post(url[,data][,fn回调函数][, dataType]);

  • 该方法与$.get()方法使用完全一致,不同的是其为post方式请求


$.ajax({  //json对象

url:请求地址,

data:给服务器传递的数据,

dataType:数据从服务器返回格式html、text、xml、json

type:get/post请求方式

success:function(){}  ajax成功请求后的回调函数,可以做后续处理使用

async:[true]异步/false同步,

cache:[true]缓存/false不缓存,

})

<!DOCTYPE html>
<html>
    <head>
        <title>php.cn</title>
        <meta charset="utf-8" />
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
        <script>
        function f1(){
            //① $.get(url[,data,fn,dataType])
            //$.get('./1.php');
            
            //$.get('./1.php','name=tom&age=20',function(msg){
            //$.get('./1.php',{name:'小强',age:21},function(msg){
            //    alert(msg)
            //});

            $.get('/1.php',{name:'小明',age:21},function(msg){
                alert(msg.name+"--"+msg.age)
            },'json');

            //$.get('./1.php',function(msg){
                //ajax完成请求的“回调函数”
                //msg是自定义变量,代表从服务器返回的信息
            //    alert(msg);
            //});
        }

        function f2(){
            //$.ajax({url: data:  dataType:  type:get/post  success:})
            $.ajax({
                url:'/1.php',
                data:{name:'小方',age:21},
                dataType:'json',
                type:'get',
                success:function(msg){
                    //msg代表服务器返回的信息
                    alert(msg.name+"--"+msg.age);
                }
            });
        }
        </script>
        <style type="text/css">
        </style>
    </head>
    <body>
        <h2>ajax请求</h2>
        <input type="button" value="请求1" onclick="f1()" />
        <input type="button" value="请求2" onclick="f2()" />
    </body>
</html>
  1. php内容为:

<?php
$name=$_GET['name'];
$age=$_GET['age'];
$arr =array(
    'name'=>$name,
    'age'=>$age
    );
echo json_encode($arr);
?>

注:大家可以将这两个文件复制到本地,放在一个文件夹中进行测试

继续学习
||
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> function f1(){ //① $.get(url[,data,fn,dataType]) //$.get('./1.php'); //$.get('./1.php','name=tom&age=20',function(msg){ //$.get('./1.php',{name:'小强',age:21},function(msg){ // alert(msg) //}); $.get('/1.php',{name:'小明',age:21},function(msg){ alert(msg.name+"--"+msg.age) },'json'); //$.get('./1.php',function(msg){ //ajax完成请求的“回调函数” //msg是自定义变量,代表从服务器返回的信息 // alert(msg); //}); } function f2(){ //$.ajax({url: data: dataType: type:get/post success:}) $.ajax({ url:'/1.php', data:{name:'小方',age:21}, dataType:'json', type:'get', success:function(msg){ //msg代表服务器返回的信息 alert(msg.name+"--"+msg.age); } }); } </script> <style type="text/css"> </style> </head> <body> <h2>ajax请求</h2> <input type="button" value="请求1" onclick="f1()" /> <input type="button" value="请求2" onclick="f2()" /> </body> </html>
提交重置代码
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!