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

  • 相关阅读:
    Java之修改文件内容:字符串逐行替换
    Java之"instanceof"和"isInstance"代码举例
    spring之跨模块引用配置文件
    Java之相对路径找不到文件问题解决方法
    html 页面模块的常用命名
    移动端资源集锦
    手机web不同屏幕字体大小高度自适应
    meta标签整理
    css文字超出自动显示省略号
    display:inline、block、inline-block的区别
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14182170.html
Copyright © 2020-2023  润新知