【问题标题】:Why does Google not support valid URLs with & in query string?为什么 Google 不支持在查询字符串中包含 & 的有效 URL?
【发布时间】:2013-11-02 11:18:38
【问题描述】:

尝试使用 W3C 验证器验证以下 HTML 将引发 3 个错误(截至 2013 年 10 月 30 日):

<!DOCTYPE html>
<head><title>asdf</title></head>
<img src="http://maps.googleapis.com/maps/api/staticmap?zoom=13&size=279x183&markers=50.916780,5.347420&sensor=false" alt="map">

三个错误都是一样的:

& 没有开始字符引用。 (& 可能应该被转义为 &。)

不幸的是,这里符合 W3C 会导致静态地图图像无法渲染。尝试嵌入带有以下 URL 的 img 标记作为其 src 属性的值:

<img src="http://maps.googleapis.com/maps/api/staticmap?zoom=13&amp;size=279x183&amp;markers=50.916780,5.347420&amp;sensor=false">

Google 会返回一个错误,说明:

Google Maps API 服务器拒绝了您的请求。无效的请求。缺少“大小”参数。

为什么 Google 静态地图 API 不支持在查询字符串中包含 &amp;amp; 的有效网址?

【问题讨论】:

    标签: url url-encoding google-static-maps


    【解决方案1】:

    您正确地转义了 &amp; 字符。

    请注意,您不能将带有转义 & 符号的 URI 直接输入到浏览器的地址栏中。它仅在 HTML 中使用时才有效(即在 hrefsrc 属性中)。

    URL in question works 完美地作为链接(也作为图像)。


    请注意,您可能 don’t have to escape 这些 & 符号(因为它们似乎是明确的);但为了始终保持安全,只需将它们全部逃脱即可。

    【讨论】:

    • 看起来 Google 现在支持 img src 属性中的转义 &amp; 字符。
    猜你喜欢
    • 2012-11-02
    • 2012-06-03
    • 2016-09-25
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多