jQuery的发送数据与转义?

编程入门 行业动态 更新时间:2024-10-20 09:31:29
本文介绍了jQuery的发送数据与转义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我听说当我使用jQuery.ajax并将数据作为对象发送时-它会自动-逃脱字符.

I heard that when i use jQuery.ajax and sending data as object - it automatically - escapes the chars.

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

where does it written ? I didnt find it in Documentation

是真的吗?

推荐答案

在源代码中,定义了本地函数add:

Inside the source code, a local function add is defined:

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

此功能通过转义特殊字符来准备任何输入.当将对象作为参数传递时,将调用buildParams方法,并传递刚刚定义的add函数:

This function prepares any input by escaping the special characters. When an object is passed as an argument, the buildParams method is invoked, passing the just-defined add function:

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

在递归函数 buildParams中,为每个对象参数调用add方法.口味有所不同,但通常采用以下格式:

Inside the recursive function buildParams, the add method is invoked for each object-parameter. The flavours differ, but are generally in the following format:

add( prefix, obj );

相关代码,这些代码源自 源代码 :


Relevant code, derived from 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 ); } }

更多推荐

jQuery的发送数据与转义?

本文发布于:2023-10-25 08:26:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!