js 发送ajax请求(XMLHttpRequest)
2016-08-26 00:17
393 查看
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
ajax({
url: "http://localhost:8080/save",
type: "POST",
data: {},
dataType: "json",
username: 'sss',
success: function(response, xml) {
// 此处放成功后执行的代码
debugger;
},
fail: function(status) {
// 此处放失败后执行的代码
debugger;
}
});
function ajax(options) {
options = options || {};
options.type = (options.type || "GET").toUpperCase();
options.dataType = options.dataType || "json";
var params = formatParams(options.data);
//创建 - 非IE6 - 第一步
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
} else {
//IE6及其以下版本浏览器
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//接收 - 第三步
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var status = xhr.status;
if (status >= 200 && status < 300) {
options.success && options.success(xhr.responseText, xhr.responseXML);
} else {
options.fail && options.fail(status);
}
}
} //连接 和 发送 - 第二步
if (options.type == "GET") {
xhr.open("GET", options.url + "?" + params, true);
xhr.send(null);
} else if (options.type == "POST") {
xhr.open("POST", options.url, true); //设置表单提交时的内容类型
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(params);
}
}
//格式化参数
function formatParams(data) {
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
}
arr.push(("v=" + Math.random()).replace(".", ""));
return arr.join("&");
}
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<title></title>
<script type="text/javascript">
ajax({
url: "http://localhost:8080/save",
type: "POST",
data: {},
dataType: "json",
username: 'sss',
success: function(response, xml) {
// 此处放成功后执行的代码
debugger;
},
fail: function(status) {
// 此处放失败后执行的代码
debugger;
}
});
function ajax(options) {
options = options || {};
options.type = (options.type || "GET").toUpperCase();
options.dataType = options.dataType || "json";
var params = formatParams(options.data);
//创建 - 非IE6 - 第一步
if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
} else {
//IE6及其以下版本浏览器
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//接收 - 第三步
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var status = xhr.status;
if (status >= 200 && status < 300) {
options.success && options.success(xhr.responseText, xhr.responseXML);
} else {
options.fail && options.fail(status);
}
}
} //连接 和 发送 - 第二步
if (options.type == "GET") {
xhr.open("GET", options.url + "?" + params, true);
xhr.send(null);
} else if (options.type == "POST") {
xhr.open("POST", options.url, true); //设置表单提交时的内容类型
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(params);
}
}
//格式化参数
function formatParams(data) {
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
}
arr.push(("v=" + Math.random()).replace(".", ""));
return arr.join("&");
}
</script>
</head>
<body>
</body>
</html>
相关文章推荐
- linux命令-----netstat , tcpdump
- Android Volley完全解析(二),使用Volley加载网络图片
- TCP/IP 纵观一览
- 循序渐进Socket网络编程(多客户端、信息共享、文件传输)
- 高仿友盟自动更新支持自定义接口的自动更新
- HTTP知识点——(三)
- 网络编程技术
- HTTP 工作原理
- HTTP 协议入门
- [转]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- 利用TCP从客户端往服务器端发送一个文件过程中发生的问题
- Deep Learning论文笔记:CNN卷积神经网络推导和实现
- win7无法启动承载网络问题
- 卷积神经网络CNN在自然语言处理中的应用
- TCP/IP 相关知识点与面试题集
- TCP连接的建立与终止
- HTTP请求报文和HTTP响应报文
- TCP三次握手和四次挥手以及缺陷(详细)
- Android——访问网络谷歌2013官方工具
- 【CV知识学习】神经网络梯度与归一化问题总结+highway network、ResNet的思考