jQuery1.9.1源码分析--Ajax模块

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:02   138   0
<div class="blogpost-body" id="cnblogs_post_body">
<div class="cnblogs_code">
  <img alt="" class="code_img_closed" id="code_img_closed_3aee2ec0-7cfe-4d23-8708-1dc394da17f1" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8f900a89c6347c561fdf2122f13be562.gif">
  <img alt="" class="code_img_opened" id="code_img_opened_3aee2ec0-7cfe-4d23-8708-1dc394da17f1" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-961ddebeb323a10fe0623af514929fc1.gif">
  <div class="cnblogs_code_hide" id="cnblogs_code_open_3aee2ec0-7cfe-4d23-8708-1dc394da17f1">
   <pre class="blockcode"><span style="color:#008080;">   1</span> <span style="color:#008000;">//</span><span style="color:#008000;">Serialize an array of form elements or a set of</span>
<span style="color:#008080;">   2</span>     <span style="color:#008000;">//</span><span style="color:#008000;">key/values into a query string</span>
<span style="color:#008080;">   3</span>     <span style="color:#008000;">//</span><span style="color:#008000;"> 将数组形式的表单元素或者哈希表序列化成字符串</span>
<span style="color:#008080;">   4</span>     jQuery.param &#61; <span style="color:#0000ff;">function</span><span style="color:#000000;">(a, traditional) {
</span><span style="color:#008080;">   5</span>         <span style="color:#0000ff;">var</span> prefix, s &#61;<span style="color:#000000;"> [],
</span><span style="color:#008080;">   6</span>             add &#61; <span style="color:#0000ff;">function</span><span style="color:#000000;">(key, value) {
</span><span style="color:#008080;">   7</span>                 <span style="color:#008000;">//</span><span style="color:#008000;"> If value is a function, invoke it and return its value</span>
<span style="color:#008080;">   8</span>                 <span style="color:#008000;">//</span><span style="color:#008000;"> 如果value是函数就执行并返回执行结果</span>
<span style="color:#008080;">   9</span>                 value &#61; jQuery.isFunction(value) ? value() : (value &#61;&#61; <span style="color:#0000ff;">null</span> ? &#39;&#39;<span style="color:#000000;"> : value);
</span><span style="color:#008080;">  10</span>                 s[s.length] &#61; encodeURIComponent(key) &#43; &#39;&#61;&#39; &#43;
<span style="color:#008080;">  11</span> <span style="color:#000000;">                    encodeURIComponent(value);
</span><span style="color:#008080;">  12</span> <span style="color:#000000;">            };
</span><span style="color:#008080;">  13</span>
<span style="color:#008080;">  14</span>         <span style="color:#008000;">//</span><span style="color:#008000;"> Set traditional to true for jQuery &lt;&#61; 1.3.2 behavior</span>
<span style="color:#008080;">  15</span>         <span style="color:#0000ff;">if</span> (traditional &#61;&#61;&#61;<span style="color:#000000;"> undefined) {
</span><span style="color:#008080;">  16</span>             traditional &#61; jQuery.ajaxSettings &amp;&amp;<span style="color:#000000;"> jQuery.ajaxSettings.traditional;
</span><span style="color:#008080;">  17</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">  18</span>
<span style="color:#008080;">  19</span>         <span style="color:#008000;">//</span><span style="color:#008000;"> If an array was passed in, assume that it is an array of form elements.</span>
<span style="color:#008080;">  20</span>         <span style="color:#008000;">//</span><span style="color:#008000;"> 如果传进来的是数组,假设是表单元素</span>
<span style="color:#008080;">  21</span>         <span style="color:#0000ff;">if</span> (jQuery.isArray(a) || (a.jquery &amp;&amp; !<span style="color:#000000;">jQuery.isPlainObject(a))) {
</span><span style="color:#008080;">  22</span>             <span style="color:#008000;">//</span><span style="color:#008000;"> 序列化表单元素</span>
<span style="color:#008080;">  23</span>             jQuery.each(a, <span style="color:#0000ff;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;">  24</span>                 add(<span style="color:#0000ff;">this</span>.name, <span style="color:#0000ff;">this</span><span style="color:#000000;">.value);
</span><span style="color:#008080;">  25</span> <span style="color:#000000;">            });
</span><span style="color:#008080;">  26</span>
<span style="color:#008080;">  27</span>         } <span style="color:#0000ff;">else</span><span style="color:#000000;"> {
</span><span style="color:#008080;">  28</span>             <span style="color:#008000;">//</span><span style="color:#008000;"> If traditional, encode the &#34;old&#34; way (the way 1.3.2 or older</span>
<span style="color:#008080;">  29</span>             <span style="color:#008000;">//</span><span style="color:#008000;"> did it), otherwise encode params recursively.</span>
<span style="color:#008080;">  30</span>             <span style="color:#0000ff;">for</span> (prefix <span style="color:#0000ff;">in</span><span style="color:#000000;"> a) {
</span><span style="color:#008080;">  31</span> <span style="color:#000000;">                buildParams(prefix, a[prefix], traditional, add);
</span><span style="color:#008080;">  32</span> <span style="color:#000000;">            }
</span><span style="color:#008080;">  33</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">  34</span>
<span style="color:#008080;">  35</span>         <span style="color:#008000;">//</span><spa
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP