我们通过ajax提交数据的时候,常常会用encodeURIComponent把参数包装一遍。
原先以为encodeURIComponent是你页面是什么编码,就会按该编码方式编码数据,但这是错的。
在ECMAScript里找到关于encodeURIComponent的说明如下:
When a character to be included in a URI is not listed above or is not intended to have the special meaning sometimes given to the reserved characters, that character must be encoded. The character is first transformed into a sequence of octets using the UTF-8 transformation, with surrogate pairs first transformed from their UCS-2 to UCS-4 encodings. (Note that for code points in the range [0,127] this results in a single octet with the same value.) The resulting sequence of octets is then transformed into a string with each octet represented by an escape sequence of the form "% xx".
于是,ajax提交中文的编码解决方案:
1.用utf-8编码方式读取传递参数
2.返回的response用utf-8编码
3.设置response header编码方式,如php下可以如此设置:header("Content-type:text/xml;charset=utf-8");
人,或多或少,总有些自以为很肯定的东西,其实是错误的认知。
分享到:
相关推荐
### encodeURIComponent与encodeURI详解 在Web开发中,处理URL参数是一项常见的任务,特别是当涉及到包含非ASCII字符(如中文)的参数时。本文将详细解释`encodeURIComponent`与`encodeURI`的区别及其应用场景,并...
同学的毕业设计出现JavaScript用encodeURIComponentt编码后无法再后台解码的问题。 原来他是这样写的: window.self.location="searchbytext.action?searchtext="+encodeURIComponent(seartext);
serialize , encodeURIComponent encodeURI 中文转成GBK编码 encodeURIComponent encodeURI 默认转 utf-8 ;重写方法 转成GBK
在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们各自有独特的用途和特点。理解它们的区别对于编写健壮的JavaScript代码至关重要。 首先,`escape()` 函数...
有三种主要的编码方法:`escape()`, `encodeURI()`, 和 `encodeURIComponent()`。它们各自有不同的应用场景和特点,下面将详细介绍这三个函数的工作原理、区别以及使用场景。 1. `escape()` `escape()` 函数主要...
### escape、encodeURI、encodeURIComponent 区别详解 在前端开发中,经常需要用到字符串编码与解码的方法来确保数据在网络传输中的正确性与安全性。本文将详细介绍 `escape()`、`encodeURI()` 和 `...
在Java编程语言中,没有内置的方法直接等同于JavaScript中的`encodeURIComponent`函数,该函数用于编码URI(统一资源标识符)组件,确保特殊字符被适当地转换为百分比编码。然而,通过组合Java的`URLEncoder`和`...
jQuery的serialize模块中有个r20正则 代码如下: var r20 = / /g, jQuery.param方法中会将所有的” ″转成”+”,即提交数据前,数据... 关于 encodeURIComponent,见MDC描述 encodeURIComponent escapes all characters
在ASP(Active Server Pages)中使用JavaScript的encodeURIComponent方法涉及到服务器端的ASP和客户端的JavaScript两种技术的结合使用。这是一种常见的跨语言交互方法,可以在服务器端代码中插入客户端JavaScript代码...
为了解决这些问题,JavaScript提供了两个内置函数:encodeURIComponent和decodeURIComponent。这两个函数对于编码和解码URL组件至关重要。本文将详细探讨这两个函数的工作原理、使用场景以及如何在实际开发中正确...
在JS中使用了encodeURIComponent对中文进行编码在PHP中使用iconv('UTF-8','gb2312',$q);就可以得到你需要的字串了,其中gb2312根据你实际应用来定如还不明白为什么看下面的
在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们用于处理和转换字符串中的特殊字符。虽然它们都与字符串编码有关,但各自的作用和适用场景有所不同。 ...
在JavaScript中,`escape`、`encodeURI`和`encodeURIComponent`是三个用于字符串编码的方法,它们各有不同的用途和特点。理解这些方法的区别对于处理URL、查询字符串和其他特殊字符的编码至关重要。 首先,`escape`...
在JavaScript中,`encodeURI()`、`encodeURIComponent()`和`escape()`是三个用于字符串编码的函数,它们的主要目的是确保特殊字符在传输过程中不会引起错误。理解它们之间的差异对于编写正确处理URL和数据的...