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

使用 axios 发起请求产生异常 #684

Closed
wssgcg1213 opened this issue Sep 15, 2021 · 3 comments
Closed

使用 axios 发起请求产生异常 #684

wssgcg1213 opened this issue Sep 15, 2021 · 3 comments
Labels
feature Request to implement a new feature.

Comments

@wssgcg1213
Copy link
Member

  • 原因是 axios 在发起调用前会判断是否跨域 isURLSameOrigin
var msie = /(msie|trident)/i.test(navigator.userAgent);
  var urlParsingNode = document.createElement('a');
  var originURL;
  /**
  * Parse a URL to discover it's components
  *
  * @param {String} url The URL to be parsed
  * @returns {Object}
  */

  function resolveURL(url) {
    var href = url;

    if (msie) {
      // IE needs attribute set twice to normalize properties
      urlParsingNode.setAttribute('href', href);
      href = urlParsingNode.href;
    }

    urlParsingNode.setAttribute('href', href); // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils

    return {
      href: urlParsingNode.href,
      protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
      host: urlParsingNode.host,
      search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
      hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
      hostname: urlParsingNode.hostname,
      port: urlParsingNode.port,
      pathname: urlParsingNode.pathname.charAt(0) === '/' ? urlParsingNode.pathname : '/' + urlParsingNode.pathname
    };
  }

  originURL = resolveURL(window.location.href);
  /**
  * Determine if a URL shares the same origin as the current location
  *
  * @param {String} requestURL The URL to test
  * @returns {boolean} True if URL shares the same origin, otherwise false
  */

  return function (requestURL) {
    var parsed = utils.isString(requestURL) ? resolveURL(requestURL) : requestURL;
    return parsed.protocol === originURL.protocol && parsed.host === originURL.host;
  };

这里 axios 使用了 a 标签的 element.pathname 来替代 new URL().pathname

  • Kraken 需要给 a 标签支持 pathname 属性
@wssgcg1213 wssgcg1213 added bug Something isn't working feature Request to implement a new feature. and removed bug Something isn't working labels Sep 15, 2021
@cnryb
Copy link
Member

cnryb commented Sep 16, 2021

#483 是相同问题,可以关掉一个

@cnryb
Copy link
Member

cnryb commented Oct 12, 2021

今天合并的 PR #723 中似乎看到了 a 标签中有了 pathname ,不知道有没有验证这个问题

@cnryb
Copy link
Member

cnryb commented Nov 16, 2021

#864 中修复了这个问题

@cnryb cnryb closed this as completed Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Request to implement a new feature.
Projects
None yet
Development

No branches or pull requests

2 participants