您的位置:首页 > Web前端 > JavaScript

javascript ajax

2016-03-07 08:42 495 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AJAX</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no"></head>
<body>

</body>

<script type = "text/javascript" >

var xmlHttpRequest = null;

function ajaxRequest(url, postParams) {
if (window.ActiveXObject) // IE浏览器
{
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) // 除IE以外的其他浏览器
{
xmlHttpRequest = new XMLHttpRequest();
}
if (null != xmlHttpRequest) {
// 准备向服务器发出一个请求

/*
* GET方式向服务器发出一个请求
* xmlHttpRequest.open("GET", "AjaxServlet?v1=" + v1 + "&v2=" + v2, true);
*/

/*
* POST方式向服务器发出一个请求
*/
xmlHttpRequest.open("POST", url, true);

// 当发生状态变化时就调用这个回调函数
xmlHttpRequest.onreadystatechange = ajaxCallBack;

// 使用post提交时必须加上下面这行代码
xmlHttpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

postParams = typeof(postParams) == "string" ? postParams : JSON.stringify(postParams);

// 向服务器发出一个请求
xmlHttpRequest.send(postParams);
}
}

function ajaxCallBack() {
console.log("[readyState] " + xmlHttpRequest.readyState);

if (xmlHttpRequest.readyState == 4) {
console.log("[status] " + xmlHttpRequest.status);

if (xmlHttpRequest.status == 200) {
console.log("[responseText] " + xmlHttpRequest.responseText);

// var content = xmlHttpRequest.responseText;
// document.getElementById("div1").innerHTML = content;
}
}
}

ajaxRequest(
"https://113.78.134.110:21900/api/trade/ptjy/ptyw/zjcx", {
"hbdm": "",
"khbz": "2006001283",
"khbzlx": "Z",
"jymm": "111111",
"sessionid": "0",
"token": "0",
"yybdm": "5010",
"lhxx": "aaaaaaa"
}
);

</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: