没有值的MediaWiki URL参数 [英] MediaWiki URL parameters without values

查看:83
本文介绍了没有值的MediaWiki URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URLquery部分似乎由由&分隔并由=关联的键值对组成.

The query part of a URL seems to consist of key-value pairs separated by & and associated by =.

我习惯于始终使用jQuery的$.param()函数对查询字符串进行URL编码,因为我发现它使我的代码更具可读性和可维护性.

I've taken to always using jQuery's $.param() function to URL-encode my query strings because I find it makes my code more readable and maintainable.

在过去的几天里,我发现自己正在调用MediaWiki API,但是当使用硬编码的URL清理工作原型以使用$.param()时,我注意到某些MediaWiki API包含带有键但不包含值的查询参数!

In the past couple of days I find myself calling the MediaWiki API but when cleaning up my working prototype with hard-coded URLs to use $.param() I noticed some MediaWiki APIs include query parameters with keys but not values!

api.php ? action=query & titles=Main%20page & redirects

请注意零件 &redirects ,该零件没有价值.

Notice the part &redirects, which takes no value.

jQuery的$.param()接受一个对象,由于对象仅由键值对组成,因此无法传递一个成员具有键但没有值的对象.

jQuery's $.param() takes an object and since objects consist only of key-value pairs it's not possible to pass an object where one member has a key but no value.

那很好,所以我假设我可以只传递一些值,例如nullundefined0,但似乎所有这些都一样.我发现这很令人惊讶,而且我无法在MediaWiki API文档中找到任何 关于其背后的原因.

That's fine so I assumed I could just pass some value in such as null or undefined or 0 but it seems all of these are treated alike. I find this surprising and I've been unable to spot anything in the MediaWiki API documentation about the reasoning behind this.

好的,在我的情况下,通过手动构建URL字符串很容易解决.我的问题是这是MediaWiki API中的一个怪癖?还是URL编码设计中的怪癖??在哪里应该阅读以了解没有关联值的URL编码参数背后的原因?

OK it's easy to work around in my case by building the URL string manually. My question is "Is this a quirk in the MediaWiki API? Or a quirk in the design of URL-encoding? Where should I read to understand the reasoning behind URL-encoded parameters that have no associated values?

推荐答案

只问了这个问题,并从其他人那里得到了一些反馈,也促使我进一步进行了研究.

Just asking this question and getting some feedback from others prompted me to dig further too.

网站表单"部分中Wikipedia的查询字符串"页面说:

  • 每个字段值对之间用等号分隔.如果该值为空字符串,则可以省略等号.
  • The field-value pairs are each separated by an equals sign. The equals sign may be omitted if the value is an empty string.

查询字符串在RFC 3986的第3.4节中定义,但在实际上,键值对不是标准的一部分,仅作简要介绍:

The query string is defined in section 3.4 of RFC 3986, but in fact the key-value pairs are not part of the standard, and are only briefly mentioned:

但是,作为查询组件 通常用于携带以下形式的识别信息: 键=值"对,一个经常使用的值是对 另一个URI,有时可用性最好避免百分比- 编码这些字符.

However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent- encoding those characters.

如您所见,键值的存在与否无关.

As you can see, nothing on the presence or absence of values for the keys.

对于jQuery,事实证明在过去15个月中已针对此行为提交了两个错误报告/功能请求:

As for jQuery, it turns out that two bug reports / feature requests have been filed about this behaviour in the past 15 months:

  • Ticket #8653 "JQUERY.PARAM OUTPUTS "NULL" AND "UNDEFINED" IN THE QUERY STRING"
  • Ticket #11329 ".PARAM() SHOULD RETURN EMPTY VALUES FOR NULL AND UNDEFINED"

提出了各种建议,涉及是否将param: nullparam: undefined转换为paramparam=.

Various proposals were made covering whether to convert param: null and param: undefined to param or param=.

最后,为下一版本的jQuery 1.8提供了一个修复程序,该修复程序将nullundefined都转换为param=-空字符串.

In the end a fix has been included for the next release of jQuery, 1.8, which converts both null and undefined to param= - the empty string.

这当然有道理,但是对于MediaWiki的情况(在错误报告/功能请求中未提及),这根本没有帮助:

This makes some kind of sense of course, but for the case of MediaWiki, which was not mentioned in the bug reports / feature requests, this does not help at all:

http://en.wikipedia.org/w/api.php?action=query&titles=Main%20page&redirects=

返回

<?xml version="1.0"?>
<api>
  <query>
    <redirects>
      <r from="Main page" to="Main Page" />
    </redirects>
    <pages>
      <page pageid="15580374" ns="0" title="Main Page" />
    </pages>
  </query>
</api>

总结:

标准没有定义应在此处执行的操作,而是将其留给实现. MediaWiki API做了一件事情,jQuery最初忽略了它,然后指出它却做了另一件事.两党似乎互不了解.

To sum up:

The standards don't define what should be done here, leaving it up to the implementations. The MediaWiki API did one thing, jQuery initially overlooked it, then when it was pointed out, did a different thing. The two parties don't seem to be aware of each other.

规范中的空白导致了不兼容的解释...但是它们并不难解决.

A gap in the spec has led to incompatible interpretations ... but they're not hard to work around.

这篇关于没有值的MediaWiki URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆