Ajax发送post请求
来源:互联网 发布:网络电视机软件下载 编辑:程序博客网 时间:2024/05/19 05:05
Ajax发送post请求
发送POST请求的注意点:
(1)请求方法为post(2)把要提交的参数放在send方法中处理(3)必须设置请求头信息 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
封装好的代码示例
// 数据处理方法function json2str(data) { var arr = []; for(var key in data){ arr.push(key+"="+data[key]); } // 不能直接将中文提交给服务器, 中文需要编码之后再提交 return encodeURI(arr.join("&"));}// 发送请求方法// function myAjax(type, url, data,timeout,success, error) {function myAjax(option) { // -1.默认值处理 option.type = option.type || "get"; option.timeout = option.timeout || 15; // 0.对参数进行处理 var res = json2str(option.data); // 1.创建异步对象 if(window.XMLHttpRequest){ var xhr = new XMLHttpRequest(); }else{ var xhr = new ActiveXObject("Microsoft.XMLHTTP"); } if(option.type == "get"){ // 2.设置URL xhr.open(option.type, option.url+"?"+res, true); // 3.发送请求 xhr.send(); }else{ // 2.设置URL xhr.open(option.type, option.url, true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // 3.发送请求 xhr.send(res); } // 4.监听状态 xhr.onreadystatechange = function () { // 5.处理返回结果 if(xhr.readyState == 4){ // 清空定时器 clearTimeout(timer); if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){ option.success(xhr); }else{ option.error(xhr.status); } } } // 6.对超时时间处理 if(!option.timeout){ return; } var timer = setTimeout(function () { alert("超时了"); // 中断请求 xhr.abort(); }, option.timeout);}
阅读全文
0 0
- Ajax发送POST请求
- Ajax发送Post请求
- Ajax -- 发送 POST 请求
- Ajax发送post请求
- ajax 发送post请求
- ajax发送get、post请求
- ajax发送get、post请求
- Post方式发送Ajax请求
- AJAX发送POST、GET请求
- ajax 页面发送post请求
- 原生ajax发送post请求
- POST方式发送ajax请求详解
- POST方式发送ajax请求详解
- POST方式发送ajax请求详解
- AJAX第二例(发送POST请求)
- POST方式发送ajax请求详解
- POST方式发送ajax请求详解
- 原生javascript实现ajax 发送post请求
- widget添加
- Import STL libraries to the Android NDK code
- 快速掌握和使用Flyway
- applicationContext.xml加载的两种方式:
- zookeeper与etcd
- Ajax发送post请求
- 【hibernate】Attribute "column" must be declared for element type "property"解决办法
- 泛型
- eclipse设置界面、字体、导入项目
- web监听器
- QFileDialog获取选择的文件
- react-native 遇到的坑汇总(补充中)
- 简单动态规划(3)——从入门到放弃
- Qt解决在其他电脑运行可执行文件