javascript js原生ajax post请求 实例
HTML代码:
注意:
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //用POST的时候一定要有这句
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content=" " />
<title>无标题 1</title>
</head>
<body>
<button onclick="ajaxp()" name="ajax" value="AJax">AJAX</button>
</body>
</html>
<script>
var xmlhttp;
function ajaxp(){
var str = "id=123";
send(str);
function send(arg) {
CreateXMLHttpRequest();
xmlhttp.onreadystatechange = callhandle;
//xmlhttp.open("GET","Default.aspx?goback=yes&arg=" + arg,true);
xmlhttp.open("POST", "ajax.php", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //用POST的时候一定要有这句
xmlhttp.send(arg);
}
function CreateXMLHttpRequest() {
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
}
function callhandle() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
}
/////
}</script>
PHP文件:
<?php
echo "i am a ajax data.". $_POST['id'];
?>
浙公网安备 33010602011771号