[JavaScript] 自定义的微型ajax框架JS代码 →→→→→进入此内容的聊天室

来自 , 2019-07-09, 写在 JavaScript, 查看 108 次.
URL http://www.code666.cn/view/6e091746
  1. var ajax = {
  2.     xhr: new XMLHttpRequest (),
  3.         request: function (method, url, vars) {
  4.                 vars = JSON.stringify(vars);
  5.                 vars = vars.replace(/\{/g, '').replace(/\}/g, '').replace(/\"/g, '').replace(/:/g, '=').replace(/,/g, '&');
  6.                
  7.                 ajax.xhr.open(method, url, true);
  8.                 ajax.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  9.                 ajax.xhr.send(vars);
  10.         },
  11.         response: function (res) {
  12.                 ajax.xhr.onreadystatechange = function () {
  13.                         return res(ajax.xhr.responseText);
  14.                 };
  15.         },
  16.         get: function(url, vars, res) {
  17.                 ajax.request('GET', url, vars);
  18.                 ajax.response(res);
  19.         },
  20.         post: function(url, vars, res) {
  21.                 ajax.request('POST', url, vars);
  22.                 ajax.response(res);
  23.         }
  24. };
  25.  
  26. /*
  27.         [object] ajax
  28.         [method] get|post
  29.                 @param [string] url
  30.                 @param [object/json] vars
  31.                 @param [function] res (callback)
  32. */
  33.  
  34. ajax.post(
  35.         'login.php',
  36.         {
  37.                 'username':'demo',
  38.                 'password':'demo'
  39.         },
  40.         function (res) {
  41.                 console.log(res);
  42.         }
  43. );
  44. This looks like a JavaScript file. Click this bar to format it.No 4
  45. //javascript/8202

回复 "自定义的微型ajax框架JS代码"

这儿你可以回复上面这条便签

captcha