原生JavaScript写AJAX
2024-09-01 23:02:44
前端JavaScript:
function ajaxGet(url, obj) {
var request;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject('Microsoft.XMLHTTP'); // 兼容IE
} request.onreadystatechange = function() {
if(request.readyState === 4) { // 4 请求完成
if(request.status === 200) { // 200 页面成功加载
console.log(request.responseText); // 成功 返回得到的文本
} else {
console.log(request.status); // 失败 返回状态码 如 404
}
} else {
console.log('Requesting');
}
}
/* 解析参数 */
str = '?';
for(key in obj) {
str += (key + '=' + obj[key] + '&');
}
str = str.substr(0, str.length - 1);
/* 发送 */
request.open('GET', url + str);
request.send();
} ajaxGet('ajax.php', {
'type': 'get',
'data': 'test'
}); //get-test
后端PHP:
<!-- ajax.php -->
<?php
echo $_GET['type'] . '-' . $_GET['data'];
最新文章
- Linux(Centos)之安装Nginx及注意事项
- 使用MapReduce实现join操作
- How to Develop blade and soul Skills
- Spring 之autowired
- go语言包与包引用
- 大话细说ORM
- js 动态计算折扣后总价格
- asp.net中的<%%>形式的详细用法实例讲解
- richTextBox1 转到行号
- 如何查詢 SQL Server 資料庫中欄位值為 NULL 的資料(转)
- sqlserver2005级联删除
- XOR 加密简介
- opencart精简checkout购物流程
- 基于SpringBoot的项目管理后台
- Nginx.conf配置文件参数说明与优化
- docker 进入容器的mongodb
- JVM java垃圾回收机制
- pycharm中conda环境部署
- Postman中使用post方式调用接口
- POJ 1328&;&;2109&;&;2586
热门文章
- linux c 获取网卡状态(UP or DOWN)
- Android代码实现控件闪烁效果
- C#创建一个Window服务
- Rest API By JAX-RS 实现接口发布
- Visual Studio提示“无法启动IIS Express Web服务器”或者“无法连接Web服务器IIS Express ”的解决方法
- CSDN开源夏令营 百度数据可视化实践 ECharts(4)
- python语言 buffer类型数据的使用 &#39;ascii&#39; codec can&#39;t decode byte 0xe5 问题的解决
- c++ about SLL(Static-Link Library) and DLL(Dynamic-Link Library)
- Shallow Heap &; Retained Heap
- 算法之动态规划(最长递增子序列——LIS)