原生ajax請求json資料

來源:互聯網
上載者:User

標籤:cli   send   字串   activex   urlencode   func   ica   html   change   

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body><button id="bt1">點擊擷取json資料</button><script type="text/javascript">    window.onload=function(){        var bt1=document.getElementById("bt1");        bt1.onclick=function(){            //建立ajax對象,寫相容            if(window.XMLHttpRequest){                var xml=new XMLHttpRequest();            }else{                var xml=new ActiveXObject("Microsoft.XMLHTTP");            };            //設定發送資料的地址和方法            xml.open("POST","json.php");            //設定我們的要求標頭資訊,post方法才寫要求標頭            xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");            //發送資料            xml.send();                        //綁定onreadystatechange事件            xml.onreadystatechange=function(){                if(xml.readyState==4 && xml.status==200){                    var data=xml.responseText;                    //json字串轉換成為json對象  , data=eval("("+data+")");evel不存在相容性問題,但是會有安全性漏洞。                    data=JSON.parse(data);                    var str="";                    str+="姓名:"+data.name+"<br>";                    str+="年齡:"+data.age+"<br>";                    str+="性別:"+data.sex;                    document.body.innerHTML=str;                };            };        };    };</script></body></html>

  

原生ajax請求json資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.