Jquery之ajax

Jquery之ajax

一、load

1、概述

2,用法

1
2
3
4
5
6
7
8
9
10
11

 

<body>
    <span id="cl">我需要陽光</span>
    <div id="dis"></div>
</body>
<script>
    $(function () {
        $("#cl").click(function(){
            $("#dis").load('http://maizi.itpaimai.com/jquery/load/');
        });
    });
</script>

二、get

1、概述

2、用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

 

<body>
<label for=""> get 的方式 獲取 手機信息</label>
<div>手機: <input type="text" name="phone" id="iphone_g" placeholder="請輸入手機號"/></div>

<div>
    <span id="get_msg">獲取的信息顯示在這裏</span>
    <br/>
    <span id="get_msg_do">點擊獲取</span>
</div>
</body>
<script>
    $(function () {
        $("#get_msg_do").click(function () {
            var iphone = $("#iphone_g").val();
            var url = 'http://maizi.itpaimai.com/jquery/get/';
            var data = {phone:iphone};
            $.get(url,data,function(msg){
                if(msg.errNum=='-1'){
                    $("#get_msg").text(msg.retMsg);
                }else{
                    $("#get_msg").text(msg.retData.supplier+':'+msg.retData.province+msg.retData.city+':'+msg.retData.suit);
                }
            },'jsonp');
        });
    });
</script>

三、post

1,概述

2、用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

 

<body>
<label for=""> post 的方式 獲取 手機信息</label>
<div>手機: <input type="text" name="phone" id="iphone_g" placeholder="請輸入手機號"/></div>

<div>
    <span id="get_msg">獲取的信息顯示在這裏</span>
    <br/>
    <span id="get_msg_do">點擊獲取</span>
</div>
</body>
<script>
    $(function () {
        $("#get_msg_do").click(function () {
            var iphone = $("#iphone_g").val();
            var url = 'http://maizi.itpaimai.com/jquery/post/';
            var data = {phone:iphone};
            $.post(url,data,function(msg){
                if(msg.errNum=='-1'){
                    $("#get_msg").text(msg.retMsg);
                }else{
                    $("#get_msg").text(msg.retData.supplier+':'+msg.retData.province+msg.retData.city+':'+msg.retData.suit);
                }
            },'json');
        });
    });
</script>

四、ajax

1、概述:

2、用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

 

<body>
<label for="">ajax 跨域方式 驗證用戶名</label>
<div>用戶名: <input type="text" id="username" placeholder="請輸入你的用戶名"/></div>
<div>
    <span id="msg">獲取的信息顯示在這裏</span>
    <br/>
    <span id="get_msg_do">點擊獲取</span>
</div>
</body>
<script>
    $(function () {
        $("#get_msg_do").click(function(){
            var name = $("#username").val();
            var url = 'http://maizi.itpaimai.com/jquery/ajax/';
            var data = {username:name};
            $.ajax({
                type:"GET",
                url:url,
                data:data,
                dataType:'jsonp',
                success:function(msgs){
                    $("#msg").text(msgs);
                },
                error:function(){
                    $("#msg").text('link fail');
                },
            });
        });
    });
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章