jQueryでカプセル化されたajax


$.get(url [,data] [,fn callback function] [, dataType]);

  • data: サーバーに渡すデータ、リクエスト文字列、jsonオブジェクトを設定可能

  • fn: コールバック関数、この関数は ajax リクエストが完了した後に呼び出され、ajax の後続の処理はこの関数内で完了できます

  • dataType: サーバーによって返されるデータ型、html、text、xml 、json

注: ajax は非同期の get リクエストです


$.post(url[,data][,fn callback function][, dataType]);

  • このメソッドは $.get() と同じです メソッドはまったく同じですが、違いはポストリクエストであることです


$.ajax({ //json object

url:リクエストアドレス、

data: サーバーに渡されるデータ、

dataType: HTML、text、xml、json の形式でサーバーから返されるデータ

type: get/post requestメソッド

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);
?>

注: これら 2 つのファイルをコピーしてローカルに移動し、テスト用のフォルダーに置くことができます

学び続ける
||
<!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 学習者の迅速な成長を支援します!