jQuery로 캡슐화된 Ajax


$.get(url [,data] [,fn 콜백 함수] [, dataType]);

  • data: 서버로 전달되는 데이터, 요청 문자열 및 json 개체를 설정할 수 있습니다

  • fn: 콜백 함수, 이 함수는 ajax 요청이 완료된 후 호출되며, 후속 ajax 처리는 이 함수에서 완료될 수 있습니다.

  • dataType: 서버에서 반환하는 데이터 유형, html, text, xml , json

참고: ajax는 비동기 가져오기 요청입니다


$.post(url[,data][,fn 콜백 함수][, dataType]);

  • 이 메서드는 $.get() 과 동일합니다. 메서드는 완전히 동일하지만, 게시물 요청이라는 점이 다릅니다


$.ajax({ //json object

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 학습자의 빠른 성장을 도와주세요!