首页 > 代码库 > Ajax : $. get()和$.post() $.getScript $.getJSON

Ajax : $. get()和$.post() $.getScript $.getJSON

<body>
    <input type="button" value="Ajax" />
    <div id="box"></div>
</body>

test.html

<span class="name">党兴明</span>
<span class="age">24</span>

test.php

<?php
    
//    if($_POST[‘age‘] == 24){
//        echo ‘php:党兴明24‘;
//    }else{
//        echo ‘木有‘;
//    }    
    
    if($_GET[‘age‘] == 24){
        echo ‘php:党兴明24‘;
    }else{
        echo ‘木有‘;
    }
    
?>

test.xml

<?xml version="1.0"?>
<root>
    <url>www.ycku.com</url>
</root>

test.josn

[
    {
        "url" : "www.ycku.com"
    }
]

$. get():

    //1 三种传值
    $(‘input‘).click(function(){
        $.get(‘test.php?age=24‘,function(response,status,xhr){
            $(‘#box‘).html(response);
        });
    });    
    //2 
    $(‘input‘).click(function(){
        $.get(‘test.php‘,‘age=24‘,function(response,status,xhr){
            $(‘#box‘).html(response);
        });
    });    
    //3
    $(‘input‘).click(function(){
        $.get(‘test.php‘,{
            age:24
        },function(response,status,xhr){
            $(‘#box‘).html(response);
        });
    });

$.post()

    //1 两种传值
    $(‘input‘).click(function(){
        $.post(‘test.php‘,‘age=24‘,function(response,status,xhr){
            $(‘#box‘).html(response);
        });
    });    
    //2
    $(‘input‘).click(function(){
        $.post(‘test.php‘,{
            age:24
        },function(response,status,xhr){
            $(‘#box‘).html(response);
        });
    });

读取.xml和.josn文件:

    $(‘input‘).click(function(){
        $.post(‘test.xml‘,function(response,status,xhr){
            $(‘#box‘).html($(response).find(‘root‘).find(‘url‘).text());
        });
    });

    $(‘input‘).click(function(){
        $.post(‘test.json‘,function(response,status,xhr){
            $(‘#box‘).html(response[0].url);
        });
    });

 $.getScript()

    $(‘input‘).click(function(){
        $.getScript(‘js/test.js‘);
    });

$.getJSON()

    $(‘input‘).click(function(){
        $.getJSON(‘test.json‘,function(response,status,xhr){
            $(‘#box‘).html(response[0].url);
        });
    });

 

Ajax : $. get()和$.post() $.getScript $.getJSON