zoukankan      html  css  js  c++  java
  • 7.15 原生js写ajax

    <!DOCTYPE html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
    </head>
    <body>
        <div id="nr">
            
        </div>
    </body>
    </html>
    <script type="text/javascript">
        //1 初始化xml http request对象
        if(window.XMLHttpRequest)
        {
            var xml = new XMLHttpRequest();
        }else
        {
            var xml = new ActiveXObject('Microsoft.XMLHTTP');//针对IE5/IE6浏览器
        }
        var attr = [1,2,3];
        //2 发送数据
        //get方式发送请求
        /*xml.open("GET","chuli.php?fname="+attr,true);   //传递的变量写到url地址
        xml.send(); */
       
        //POST方式发送请求
        xml.open("POST","chuli.php",true);
        xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");  //传值时,要有这一句
        xml.send("fname="+attr);
        //3接受返回值
        xml.onreadystatechange=function()
        {
            if(xml.readyState==4 && xml.status==200)
            {
                var str=xml.responseText;//以字符串形式传回的,所以处理页面也要把数据整合成字符串
                console.log(str);
            }
            //document.getElementById("nr").innerHTML=str;    //找到id元素,添加返回的数据
        }
    </script>

    chuli.php

    <?php
       $attr = $_POST['fname'];
       //$str = join(",",$attr);
       echo $_POST['fname'];
    ?>
  • 相关阅读:
    149. Max Points on a Line(js)
    148. Sort List(js)
    147. Insertion Sort List(js)
    146. LRU Cache(js)
    145. Binary Tree Postorder Traversal(js)
    144. Binary Tree Preorder Traversal(js)
    143. Reorder List(js)
    142. Linked List Cycle II(js)
    141. Linked List Cycle(js)
    140. Word Break II(js)
  • 原文地址:https://www.cnblogs.com/sunhao1987/p/9314375.html
Copyright © 2011-2022 走看看