export function updateProgress(event: any) {
  if (event.lengthComputable) {
    var percentComplete = event.loaded / event.total;
  }
}

export function xhr() {
  var xhr = new XMLHttpRequest();
  return {
    request: (method: any, url: any, data: any, success: any, err: any) => {
      xhr.open(method, url);
      xhr.onprogress = updateProgress;
      xhr.upload.onprogress = updateProgress;
      if (method == 'GET') {
        xhr.send();
      } else {
        xhr.setRequestHeader(
          'Content-Type',
          'application/x-www-form-urlencoded'
        );
        // xhr.setRequestHeader('test', 'test');
        // xhr.setRequestHeader('Host', '192.168.1.1:5000');
        // xhr.setRequestHeader('Origin', 'http://192.168.1.1:5000');
        // xhr.setRequestHeader('Referer', 'http://192.168.1.1:5000/upload');
        // 声明请求源
        xhr.setRequestHeader('Origin', 'http://a.example.com');
        xhr.send(data);
      }
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          success(xhr.responseText);
        } else {
          err();
        }
      };
    },
  };
}
