• 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>
     
    参考:
     
     
     
     
     
     
     
     

  • 相关阅读:
    Best Time to Buy and Sell Stock II
    Subsets II
    Subsets I
    Combinations
    Permutation Sequence
    Next Permutation
    Anagrams
    Combination-Sum II
    Combination-Sum I
    Permutations II
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182170.html
Copyright © 2020-2023  润新知