Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 65 期(W3C 标准-JavaScript-异步):手写实现AJAX #68

Open
wingmeng opened this issue Jul 21, 2019 · 0 comments
Open

第 65 期(W3C 标准-JavaScript-异步):手写实现AJAX #68

wingmeng opened this issue Jul 21, 2019 · 0 comments

Comments

@wingmeng
Copy link
Collaborator

任务:
手写实现 AJAX

代码:

/**
 * @param {string} url - 请求的url地址
 * @param {string} method - 请求方式,get(默认) || post
 * @param {function} callback - 回调函数
 */
function sendAjaxRequest(url, method, callback) {
  var xhr = null;
  method = method || 'get';
  callback = typeof callback === 'function' ? callback : function() {};

  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else {
    xhr = new ActiveObject('Microsoft.XMLHTTP');  // 兼容低版本IE
  }

  xhr.open(method, url, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        var data = JSON.parse(xhr.responseText);
        callback(data);
      }
    }
  }
  xhr.send(null);
}
@wingmeng wingmeng changed the title 第 65 期(W3C 标准-JavaScript-AJAX):手写实现AJAX 第 65 期(W3C 标准-JavaScript-异步):手写实现AJAX Jul 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant