天天看点

jquery+json的6种ajax请求

跨域的方法

jsonp跨域get请求

通过iframe实现跨域

flash跨域http请求

window.postmessage

header实现跨域

serialize序列表表格内容为字符串,不支持form嵌套中的子form

html代码:

jquery+json的6种ajax请求

<form id="myform">  

姓名:<input type="text" name="name" size="10" />  

性别:<input type="radio" name="sex" value="男" />男  

<input type="radio" name="sex" value="女" />女  

<input type="submit" value="ok" />  

</form>  

<div id="rename"></div>  

<div id="resex"></div>  

<div id="re" style="display:none"></div>  

 show.php代码:

jquery+json的6种ajax请求

<?php  

header("cache-control: no-cache, no-store, must-revalidate, max-age=-1");  

header("content-type: application/json; charset=utf-8");  

$user['name']="姓名:".$_request['name'];  

$user['sex']="性别:".$_request['sex'];  

echo json_encode($user);  

?>  

jquery+json的6种ajax请求

<script type="text/javascript">  

    $(document).ready(function () {  

        $(":submit").click(function () {  

            var str = $("#myform").serialize();  

            $.ajax({  

                url:"show.php",  

                type:"post",  

                data:str,  

                datatype:"json",  

                timeout:1000,  

                error:function () {  

                    alert("ajax error!");  

                },  

                success:callback  

            });  

            return false;  

        });  

    });  

    function callback(jsondata) {  

        $("#rename").html(jsondata.name);  

        $("#resex").html(jsondata.sex);  

    }  

</script>  

 第二种方式:2、load(url,data,callback)

jquery+json的6种ajax请求

$(document).ready(function(){  

    $(":submit").click(function(){  

        var str=$("#myform").serialize();  

        $("#re").load("show.php",str,function(jsondata){  

            data=eval("("+jsondata+")");  

            $("#rename").html(jsondata.name);  

            $("#resex").html(jsondata.sex);  

        return false;  

});  

load方式可以在url后面加选择器的,语法形如 "url #some > selector",但这里的例子返回的数据是json格式,所以就不能加选择器,这里只是为了演示,所以就用一个隐藏的div来载入json数据,然后 在回调函数里显示出来,在实际项目中是不会这样整的。。。

第三种方式:3、jquery.get(url,data,callback,type)

jquery+json的6种ajax请求

        $.get("show.php",str,function(jsondata){  

        },"json");  

 第四种方式:4、jquery.getjson(url,data,callback) 异步请求

jquery+json的6种ajax请求

        $.getjson("show.php",str,function(jsondata){  

</script>   

getjson还可以通过jsonp协议来进行跨域调用数据。只要用jsonp方式就只能是get,因为本质是script方式加载的一个简单的例子:

jquery+json的6种ajax请求

$.getjson("http://www.b.com/server.php?name=jackie&sex=boy&callback=?",function(data){  

    alert(data.name+" "+data.sex);  

jquery+json的6种ajax请求

    $name=$_request['name'];  

    $sex=$_request['sex'];  

    $jsondata="{name:'".$name."',sex:'".$sex."'}";  

    echo $_get['callback']."(".$jsondata.")";  

jquery+json的6种ajax请求

callback({name:'this is json data',sex:'男'})  

 header实现跨域

jquery+json的6种ajax请求

// ajax通过设置access-control-allow-origin来实现跨域访问比较简单.指定允许其他域名访问  

header('access-control-allow-origin:*');  

// 响应类型  

header('access-control-allow-methods:post, get, options');  

// 响应头设置  

header('access-control-allow-headers:x-requested-with,content-type');  

第五种方式:5、jquery.post(url,data,callback,type)

jquery+json的6种ajax请求

        $.post("show.php",str,function(jsondata){  

        },"json")  

 第六种方式:jquery.getscript(url,[callback])

jquery+json的6种ajax请求

<html>  

<head>  

<title>jquery学习</title>  

<script src="jquery-1.2.6.min.js" type="text/javascript"></script>  

    var obtntest = $("#btntest");  

    obtntest.click(function(){  

        obtntest.disabled = true;  

        var oresult = $("#result");  

        oresult.html("loading").css("color","red");  

        jquery.getscript("http://app.cntvs.com/test/js.txt",  

        function(){  

            oresult.html("name:" + jimmy.name + "<br/>email:" + jimmy.email).css("color","black");  

            obtntest.disabled = false;  

</head>  

<body>  

<button id="btntest">btntest</button>  

<div id="result"></div>  

</body>  

</html>  

 远程服务器端js.txt的内容为:

var jimmy = {name:"jimmy.yang",email:[email protected]}