【问题标题】:DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL In React AppDOMException:无法在“XMLHttpRequest”上执行“打开”:React 应用程序中的 URL 无效
【发布时间】:2020-07-18 13:45:40
【问题描述】:

我正面临一个神秘的错误。当我尝试向服务器发送一个特定查询时,我收到以下错误

DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
at dispatchXhrRequest (http://localhost:6789/static/js/20.chunk.js:180790:13)
at new Promise (<anonymous>)
at xhrAdapter (http://localhost:6789/static/js/20.chunk.js:180773:10)
at dispatchRequest (http://localhost:6789/static/js/20.chunk.js:181396:10)

在这里我记录了实际的 url 字符串

http://localhost:3000​/users​/5​/payments​/disconnect

如果我发送相同的查询但从我的主机到开发服务器,我会看到错误 404 和这样的 url

https://example.com/users%E2%80%8B/5%E2%80%8B/payments%E2%80%8B/disconnect

这是我发送查询的函数

  const query = `${url}​/users​/${userId}​/payments​/disconnect`;

  console.debug('Url', query);

  try {
    const { status } = await axios.delete(query);
    return status;
  } catch (e) {
    console.debug(e);
    return 500;
  }
}

有趣的是,所有其他端点都可以正常工作。在 chrome 和 firefox 上的行为相同。

什么会导致这样的问题?

【问题讨论】:

    标签: reactjs rest axios nestjs


    【解决方案1】:

    %E2%80%8B 是一个 U+200B : ZERO WIDTH SPACE,你设法用它污染了你的字符串。

    如果您将const query = `${url}​/users​/${userId}​/payments​/disconnect`; 行复制/粘贴到this app,您可以在源代码中看到它们。

    通过像这样编辑该行可能最容易删除它:

    1. 将光标定位在字符之后/之后
    2. Backspace直到删除/之前的字符
    3. 重新键入所需的三个字符

    【讨论】:

    • 它有效。万分感谢!那我就不明白了,这个字符怎么会出现在代码中?
    • @IvanBanha — 很可能:它是输入或复制/粘贴的。
    • 哇,难以置信,但这实际上是我的问题。我的 URL 字符串中有两个不可见的双字节字符 (U+200B : ZERO WIDTH SPACE [ZWSP])。谢谢@Quentin
    猜你喜欢
    • 2017-08-15
    • 2017-07-16
    • 2019-09-25
    • 2019-12-01
    • 2016-12-03
    • 1970-01-01
    • 2019-04-27
    • 2016-05-16
    • 1970-01-01
    相关资源
    最近更新 更多