您好,欢迎访问一九零五行业门户网

关于Ajax的get和post请求的介绍

下面为大家带来一篇深入理解ajax的get和post请求。内容挺不错的,现在就分享给大家,也给大家做个参考。
1.get请求
function () { //向服务器请求 时间 //1.创建异步对象(小浏览器) var xhr = new xmlhttprequest(); //2.设置参数,true表示使用异步模式 xhr.open("get", "gettime.ashx?name= mr靖", true); //3.让get请求不从浏览器获取缓存数据 xhr.setrequestheader("if-modified-since","0"); //3.设置回调函数 xhr.onreadystatechange = function () { //3.1当完全接收完响应报文后 并且 响应状态码为200的时候 if (xhr.readystate == 4 && xhr.status == 200) { //3.2获取相应报文体内容 var res = xhr.responsetext; alert(res); } }; //4.发送异步请求 xhr.send(null);}
2.post请求
function () { //向服务器请求 时间 //1.创建异步对象(小浏览器) var xhr = new xmlhttprequest(); //2.设置参数 xhr.open("post", "gettime.ashx", true); //3.设置 请求 报文体 的 编码格式(设置为 表单默认编码格式) xhr.setrequestheader("content-type", "application/x-www-form-urlencoded"); //4.设置回调函数 xhr.onreadystatechange = function () { //3.1当完全接收完响应报文后 并且 响应状态码为200的时候 if (xhr.readystate == 4 && xhr.status == 200) { //3.2获取相应报文体内容 var res = xhr.responsetext; alert(res); } }; //5.发送异步请求"name=mr靖" //5.1格式:直接拼接字符串 key=value&key1=value2 xhr.send("name=mr靖&age=18"); };
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
ajax使用post发送数据xml格式接受数据
对jquery中的ajax再封装和简化的操作
以上就是关于ajax的get和post请求的介绍的详细内容。
其它类似信息

推荐信息