zoukankan      html  css  js  c++  java
  • ajax post方式传递参数

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>JS</title>
        <style>
            #box{
                width:600px;
                height:200px;
                padding:20px;
                border:1px solid #999;
            }
        </style>
    </head>
    <body>
        <h1>ajax post方式传递参数</h1>
        <hr>
        Number1: <input type="text" id="num1"><br>
        Number2: <input type="text" id="num2"><br>
        <button onclick="loadHtml()">加载</button>
        <div id="box"></div>
        <script>
            function loadHtml(){
                //获取表单中的数据
                var num1 = document.getElementById('num1').value;
                var num2 = document.getElementById('num2').value;
    
                //实例化XMLHttpRequest对象
                if(window.XMLHttpRequest){
                    //非IE 
                    var xhr = new XMLHttpRequest();
                }else{
                    //IE 6
                    //var xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    var xhr = new ActiveXObject('Microsoft.XMLHTTP');
                }
                //给xhr绑定事件.检测请求的过程
                xhr.onreadystatechange = function(){
                    console.log(xhr.readyState);
                    //如果成功接收到并响应
                    if(xhr.status == 200 && xhr.readyState == 4){
                        document.getElementById('box').innerHTML = xhr.responseText;
                    }
                }
                //进行请求的初始化
                xhr.open('post','js.php',true);
                //设置请求头
                xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
                //正式发送请求
                xhr.send('n1='+num1+'&n2='+num2);
            }
        </script>
    </body>
    </html>

    js.php

    <?php 
    echo $_POST['n1'] + $_POST['n2'];
     ?>
  • 相关阅读:
    jQuery live事件说明及移除live事件方法
    Jquery的html方法里包含特殊字符的处理
    mysql创建定时任务
    MySQL内置函数获取几天前的日期
    实战mysql分区
    TCP的TIME_WAIT状态
    openssl生成SSL证书的流程
    mysql备份的三种方式详解
    mysql创建唯一索引
    MYSQL双机热备份的配置实施(问题总结)
  • 原文地址:https://www.cnblogs.com/shanyansheng/p/5125897.html
Copyright © 2011-2022 走看看