2012-01-05 74 views
2

我听说,当我使用jQuery.ajax和发送数据作为对象 - 它会自动 - 转义字符。jquery发送带有转义数据?

它写在哪里? 我没有在文档中找到它

它是真的吗?

+0

是的,这是真的。如果在文档中找不到它,请查看源代码。编辑:我已经在** [源代码](http://code.jquery.com/jquery-1.7.1.js)**:s [s.length] = encodeURIComponent(key)+“=” + encodeURIComponent(value);' – 2012-01-05 16:20:02

+0

releated:http://stackoverflow.com/questions/2231810/escaping-jquery-data-being-sent-via-post – 2012-01-05 16:21:10

+0

可能的重复[如何正确地转义HTML作为数据发送jQuery的.ajax函数](http://stackoverflow.com/questions/4122298/how-to-properly-escape-html-sent-as-data-in-jquerys-ajax-function) – 2012-01-05 16:22:23

回答

1

的源代码的内部,一个局部函数add定义:

add = function(key, value) { 
    value = jQuery.isFunction(value) ? value() : value; 
    s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value); 
}; 

此功能通过转义特殊字符准备的任何输入。当对象被作为参数传递,所述buildParams方法被调用时,使刚刚定义add功能:

for (var prefix in a) { 
    buildParams(prefix, a[ prefix ], traditional, add); 
} 

里面的递归函数buildParams,所述add方法被调用用于每个对象的参数。味道不同,但一般都在以下格式:

add(prefix, obj); 


相关代码,从 the source code导出:

// Serialize an array of form elements or a set of 
    // key/values into a query string 
    param: function(a, traditional) { 
     var s = [], 
      add = function(key, value) { 
       // If value is a function, invoke it and return its value 
       value = jQuery.isFunction(value) ? value() : value; 
       s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value); 
      }; 

     // Set traditional to true for jQuery <= 1.3.2 behavior. 
     if (traditional === undefined) { 
      traditional = jQuery.ajaxSettings.traditional; 
     } 

     // If an array was passed in, assume that it is an array of form elements. 
     if (jQuery.isArray(a) || (a.jquery && !jQuery.isPlainObject(a))) { 
      // Serialize the form elements 
      jQuery.each(a, function() { 
       add(this.name, this.value); 
      }); 

     } else { 
      // If traditional, encode the "old" way (the way 1.3.2 or older 
      // did it), otherwise encode params recursively. 
      for (var prefix in a) { 
       buildParams(prefix, a[ prefix ], traditional, add); 
      } 
     } 

     // Return the resulting serialization 
     return s.join("&").replace(r20, "+"); 
    } 
}); 

function buildParams(prefix, obj, traditional, add) { 
    if (jQuery.isArray(obj)) { 
     // Serialize array item. 
     jQuery.each(obj, function(i, v) { 
      if (traditional || rbracket.test(prefix)) { 
       // Treat each array item as a scalar. 
       add(prefix, v); 

      } else { 
       // If array item is non-scalar (array or object), encode its 
       // numeric index to resolve deserialization ambiguity issues. 
       // Note that rack (as of 1.0.0) can't currently deserialize 
       // nested arrays properly, and attempting to do so may cause 
       // a server error. Possible fixes are to modify rack's 
       // deserialization algorithm or to provide an option or flag 
       // to force array serialization to be shallow. 
       buildParams(prefix + "[" + (typeof v === "object" || jQuery.isArray(v) ? i : "") + "]", v, traditional, add); 
      } 
     }); 

    } else if (!traditional && obj != null && typeof obj === "object") { 
     // Serialize object item. 
     for (var name in obj) { 
      buildParams(prefix + "[" + name + "]", obj[ name ], traditional, add); 
     } 

    } else { 
     // Serialize scalar item. 
     add(prefix, obj); 
    } 
} 
1

这隐含地假设。通常,只要你有一个函数可以传输来自某个对象或参数的数据,就可以假设该函数能够正确地转义/参数化数据,以便传递任意字符串。

假设你使用的是良好的库(jQuery是),你应该只需要在明确构建字符串时转义一些东西。

例如,jQuery的text()函数会自动HTML转义您的文本。