zoukankan      html  css  js  c++  java
  • ajax异步post方法

    HTML

    <input type="button" name="btn" id="btn" value="按钮" />
    

    JS

    var oBtn=document.getElementById("btn");
    
    oBtn.onclick=function(){
    	var xhr=null;
    	try{
    		xhr=new XMLHttpRequest();
    	}catch(e){
    		xhr=new ActiveXObject('Microsoft.XMLHTTP');
    	}
    	/*
    	 post方式,数据放在send()里面作为参数传递
    	 post方式没有缓存问题
    	 post方式无需编码
    	 * */
    	xhr.open('post','post.php',true);
    	//申明发送的数据类型
    	xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
    	//提交  发送请求
    	xhr.send('username=刘伟&age=30');
    	
    	//等待服务器返回内容
    	xhr.onreadystatechange=function(){
    		if(xhr.readyState==4){
    			if(xhr.status==200){
    				alert(xhr.responseText);
    			}else{
    				alert('出错了,Err'+xhr.status);
    			}
    			
    		}
    	}
    }
    

    PHP

    <?php
    header('content-type:text/html;charset="utf-8"');
    error_reporting(0);
     
    $username=$_POST['username'];
    $age=$_POST['age'];
     
    echo "你的名字:{$username},年龄:{$age}";
    ?>
    

      

  • 相关阅读:
    Python--day72--ajax简介
    Python基础1 介绍、基本语法
    10-Python-字符编码
    09-Python-集合
    07-Python-字符串操作
    99-Python-深浅拷贝
    06-Python-字典
    05-Python-判断语句
    04-Python-元组
    03-Python-操作列表
  • 原文地址:https://www.cnblogs.com/yangxue72/p/8241632.html
Copyright © 2011-2022 走看看