原生js ajax封装post,get方法

冷不防 2022-09-08 11:49 310阅读 0赞

方法一:

  1. function ajax(method, url, callback, data, flag) {
  2. var xhr;
  3. flag = flag || true;
  4. method = method.toUpperCase();
  5. if (window.XMLHttpRequest) {
  6. xhr = new XMLHttpRequest();
  7. } else {
  8. xhr = new ActiveXObject('Microsoft.XMLHttp');
  9. }
  10. xhr.onreadystatechange = function () {
  11. if (xhr.readyState == 4 && xhr.status == 200) {
  12. console.log(2)
  13. callback(xhr.responseText);
  14. }
  15. }
  16. if (method == 'GET') {
  17. var date = new Date(),
  18. timer = date.getTime();
  19. xhr.open('GET', url + '?' + data + '&timer' + timer, flag);
  20. xhr.send()
  21. } else if (method == 'POST') {
  22. xhr.open('POST', url, flag);
  23. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  24. xhr.send(data);
  25. }
  26. }

方法二:

  1. function serialize(data){//对发送数据的序列化
  2. if(!data) return '';
  3. var pairs=[];
  4. for(var name in data){
  5. if(!data.hasOwnProperty(name)) continue;//排除嵌套对象
  6. if(typeof data[name]==='function') continue;//排除操作数是函数
  7. var value=data[name].toString();
  8. //encodeURIComponent对同一资源表示符(URI)编码,
  9. name=encodeURIComponent(name);
  10. valeu=encodeURIComponent(value);
  11. pairs.push(name+'='+value);
  12. }
  13. return pairs.join('&');
  14. }
  15. //get方法封装
  16. function get(url,options,callback){
  17. if(window.XMLHttpRequest){
  18. var xhr=new XMLHttpRequest();
  19. }else if(window.ActiveXobject){//兼容IE7及以前版本
  20. var xhr=new ActiveXobject('Microft.XMLHttp');
  21. }
  22. xhr.onreadystatechange=function(callback){
  23. if(xhr.readyState==4){//请求结束时,readyState状态为4
  24. if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
  25. callback(xhr.responseText);
  26. }else{
  27. console.log('连接失败:'+xhr.status);
  28. }
  29. }
  30. }
  31. // http://localhost:8000/information?name=zhou&age=18
  32. url=url+'?'+serialize(options);
  33. xhr.open('get',url,true);//开启请求,readyState状态为1
  34. xhr.send(null);//正式像服务器发送请求,readyState状态为2
  35. }
  36. //post方法封装
  37. function post(url,options,callback){
  38. if(window.XMLHttpRequest){
  39. var xhr=new XMLHttpRequest();
  40. }else if(window.ActiveXobject){//兼容IE7及以前版本
  41. var xhr=new ActiveXobject('Microft.XMLHttp');
  42. }
  43. xhr.onreadystatechange=function(callback){
  44. if(xhr.readyState==4){//请求结束时,readyState状态为4
  45. if((xhr.status>=200&&xhr.status<300)||xhr.status==304){
  46. callback(xhr.responseText);
  47. }else{
  48. console.log('POST请求连接失败:'+xhr.status);
  49. }
  50. }
  51. }
  52. xhr.open('post',url,true);//开启请求,readyState状态为1
  53. xhr.send(serialize(options));//正式像服务器发送请求,readyState状态为2
  54. }
  55. post('/information',{name:'zhou',age:18},function(data){
  56. console.log(data);
  57. });
  58. get('/information',{name:'zhou',age:18},function(data){
  59. console.log(data);
  60. });

异步加载javascript:

  1. function loadScript(url, callback) {
  2. var oscript = document.createElement('script');
  3. if (oscript.readyState) { // ie8及以下版本
  4. oscript.onreadystatechange = function () {
  5. if (oscript.readyState === 'complete' || oscript.readyState === 'loaded') {
  6. callback();
  7. }
  8. }
  9. } else {
  10. oscript.onload = function () {
  11. callback()
  12. };
  13. }
  14. oscript.src = url;
  15. document.body.appendChild(oscript);
  16. }

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读

    相关 原生js封装ajax方法

    jquery框架的ajax方法固然好用,但是假如某天我们的项目不能引入jquery或项目需求很简单,没有很多交互功能,只需要ajax,这时引入jquery库会造成资源浪费,也会

    相关 原生JS封装AJAX方法

    原生JS封装AJAX方法 Ajax大家每天都在用,jquery库对Ajax的封装也很完善、很好用,下面我们看一下它的内部原理,并手动封装一个自己的Ajax库。 一、原理 ...