使 URL W3C 有效并在 Ajax 请求中工作
我有一个返回 URL 的通用函数。 (它是一个插件函数,可返回插件内资源 [图像、样式表] 的 URL)。
我在这些 URL 中使用 GET 参数。
如果我想在 HTML 页面中使用这些 URL,以通过 W3C 验证,我需要将 & 符号屏蔽为 &
/plugin.php?plugin=xyz&resource=stylesheet&....
但是,如果我想使用 URL 作为 AJAX 调用的“url”参数,则 & 符号不会被正确解释,从而搞砸了我的调用。
我可以做点什么吗?在 AJAX 调用中工作?
我非常希望避免向 URL 生成函数添加参数(intendedUse="ajax" 或其他)或在 Javascript 中操作 URL,因为此插件模型将被重复使用多次(可能是很多人)并且我希望它尽可能简单。
I have a generic function that returns URLs. (It's a plugin function that returns URLs to resources [images, stylesheets] within a plugin).
I use GET parameters in those URLs.
If I want to use these URLs within a HTML page, to pass W3C validation, I need to mask ampersands as &
/plugin.php?plugin=xyz&resource=stylesheet&....
but, if I want to use the URL as the "url" parameter for a AJAX call, the ampersand is not interpreted correctly, screwing up my calls.
Can I do something get & work in AJAX calls?
I would very much like to avoid adding parameters to th URL generating function (intendedUse="ajax" or whatever) or manipulating the URL in Javascript, as this plugin model will be re-used many times (and possibly by many people) and I want it as simple as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,您遇到了让应用程序的一个部分跨多个层的问题。在本例中,它是插件。
RFC 1738 指定的 URL 规定 URL 应使用
& ;
标记,用于将键/值对彼此分开。然而,& 符号是 HTML 中的保留标记,因此应转义为&
。由于转义 & 符号是 HTML 的产物,因此您的插件可能不应该直接转义它们。相反,您应该有一个函数或某种转义规范 URL 的东西,以便它可以嵌入 HTML 标记中。It seems to me that you're running into the problem of having one piece of your application cross multiple layers. In this case it's the plugin.
A URL as specified by RFC 1738 states that a URL should use a
&
token to separate key/value pairs from one another. However ampersand is a reserved token in HTML and therefore should be escaped into&
. Since escaping the ampersands is an artifact of HTML, your plugin should probably not be escaping them directly. Instead you should have a function or something that escapes a canonical URL so that it can be embedded in HTML markup.唯一可能实际发生这种情况的地方是,如果您是:
这不是一个愉快的组合,解决方案是 在规范中。
XHTML 媒体类型注释包含相同的建议,但还提供了解决方法 如果您选择忽略它。
The only place that this is likely to actually happen is if you are:
<script>
This is not a happy combination, and the solution is in the spec.
The XHTML media types note includes the same advice, but also provides a workaround if you choose to ignore it.
尝试返回 JSON 而不仅仅是字符串,这样您的 Javascript 就可以将 URL 值作为对象读取,并且您不应该遇到这个问题。除此之外,尝试简单地对字符串进行 HTML 解码,使用类似以下内容:
显然,您需要确保删除对可能创建的 DOM 元素的任何引用(为了简化示例,我在这里没有这样做)。
我在工作中创建的 AJAX 站点中使用了这种技术,并多次使用它来解决这个问题。
Try returning JSON instead of just a string, that way your Javascript can read the URL value as an object, and you shouldn't have that issue. Other than that, try simply HTML decoding the string, using something like:
Obviously you'll want to make sure you remove any reference to DOM elements you might create (which I've not done here to simplify the example).
I use this technique in the AJAX sites I create at my work and have used it many times to solve this problem.
当您具有以下形式的标记时:
那么
href
属性的值为?a=1&b=2
。&
只是 HTML/XML 中的转义序列,不会影响属性的值。这类似于:其中属性的值为
<>
。相反,如果您有以下形式的代码:
那么您可以使用 JavaScript 函数:
这允许原本仅是有效 HTML 或仅有效 XHTML 的代码在两者中均有效。 (有关更多信息,请参阅 Dorwald 的评论。)
When you have markup of the form:
Then the value of the
href
attribute is?a=1&b=2
. The&
is only an escape sequence in HTML/XML and doesn't affect the value of the attribute. This is similar to:Where the value of the attribute is
<>
.If, instead, you have code of the form:
Then you can use a JavaScript function:
This allows code that would otherwise only be valid HTML or only valid XHTML to be valid in both. (See Dorwald's comments for more info.)