jQuery 发送 ajax json 请求

code1
function (data) {
    data = data || {}
 
 
    $.ajax({
        type: "POST",
        url: data.url,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(data.data),
        success: function (resp) {
            data.success(resp)
        },
        error: function (err) {
            data.error(err)
        }
    })
}
code2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
    document.getElementById("getXML").onclick=getLabelsGet;
    /* 创建 XMLHttpRequest 对象 */
    var xmlHttp;
    function GetXmlHttpObject(){
      if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      return xmlhttp;
    }
    // -----------ajax方法-----------//
    function getLabelsGet(){
      xmlHttp=GetXmlHttpObject();
      if (xmlHttp==null){
        alert('您的浏览器不支持AJAX!');
        return;
      }
      var id = document.getElementById('id').value;
      var url="http://www.Leefrom.com?id="+id+"&t/"+Math.random();
      xmlHttp.open("GET",url,true);
      xmlHttp.onreadystatechange=getOkGet;//发送事件后,收到信息了调用函数
      xmlHttp.send();
    }
 
 
    function getOkGet(){
      if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){
            console.log("加载中...");
      }
      if (xmlHttp.readyState==4 && xmlHttp.status==200){
            var d= xmlHttp.responseText;
            console.log(d);
      }
    }
    </script>
</head>
<body>
    <input type="button"  value="获取XMl数据" id='getXML'>
</body>
</html>
 

 

参考:
 
 
 
 
 
 
 
 

posted @ 2020-12-23 23:52  anobscureretreat  阅读(328)  评论(0编辑  收藏  举报