无法将变量传递给标头(XMLHttpRequest)

时间:2017-09-06 15:03:31

标签: javascript request

uploadImageCallBack(file) {

     var token = ' eyJhbG6Aw5/MwymOfs4B76D5JEr/o0YymuXZufo='
  return new Promise(
    (resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://dev.hylaa.net/oa/file/upload');
      xhr.setRequestHeader('Authorization':'Bearer ' + token);
      xhr.setRequestHeader('Content-Type': 'multipart/form-data');
      const data = new FormData();
      data.append('image', file);
      xhr.send(data);
      xhr.addEventListener('load', () => {
        const response = JSON.parse(xhr.responseText);
        resolve(response);
      });
      xhr.addEventListener('error', () => {
        const error = JSON.parse(xhr.responseText);
        reject(error);
      });
    }
  );
}

无法传递变量"令牌"标题

SyntaxError:意外的令牌,预期,

我错了吗?当我使用AXIOS时,我可以将变量传递给此标头

1 个答案:

答案 0 :(得分:1)

您有一个label,您没有正确设置标题。应该是("header", "value")

xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.setRequestHeader('Content-type', 'multipart/form-data');