2012-01-05 72 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轉義您的文本。