2
A
回答
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转义您的文本。
相关问题
- 1. jQuery Ajax发送空的转义数据
- 2. 通过POST转义jQuery数据发送
- 3. 从jquery数据表发送带有ajax调用的参数
- 4. 转义用户发送数据?
- 5. jquery没有发送发布数据
- 6. jquery发送发布数据,但没有定义url
- 7. 发送带有文本数据的AJAX请求(jQuery)
- 8. 使用AJAX和JQuery发送带有JSON数据的PUT调用?
- 9. jQuery的S.get发送带有装饰()数据
- 10. 发送数据从jQuery发送到phpexcel
- 11. jQuery没有发送数据到codeigniter
- 12. jquery发送数据到php没有ajax
- 13. jQuery传送带转换
- 14. jQuery UI选项卡:如何发送带有发布数据的ajax请求?
- 15. 加载页面时,如何在jquery中发送带有ajax的发布数据?
- 16. 发送带有RakNet
- 17. 发送带有MPDF
- 18. AngularJS:PUT发送带有URL数据,但不能作为JSON数据
- 19. 使用JSON发送带有数据的推送通知
- 20. jQuery发送空数据
- 21. Jquery .post不发送数据
- 22. 用jQuery发送JSON数据
- 23. 发送数据通过jQuery
- 24. jQuery ajax,发送数据
- 25. jQuery barrating.js - 发送ID数据
- 26. 发送带有LINK_TO和数据没有的TurboLink
- 27. 发送带有表格数据的javascript数组
- 28. 发送带有IP_TOS辅助数据的UDP数据包时sendmsg()失败,并且发送UDP数据包
- 29. 改造1,发送带有自定义按键多形式的数据值
- 30. jQuery/ajax不会发送发布数据
是的,这是真的。如果在文档中找不到它,请查看源代码。编辑:我已经在** [源代码](http://code.jquery.com/jquery-1.7.1.js)**:s [s.length] = encodeURIComponent(key)+“=” + encodeURIComponent(value);' – 2012-01-05 16:20:02
releated:http://stackoverflow.com/questions/2231810/escaping-jquery-data-being-sent-via-post – 2012-01-05 16:21:10
可能的重复[如何正确地转义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