当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 保证JavaScript和Asp、Php等后端程序间传值编码统一

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 保证JavaScript和Asp、Php等后端程序间传值编码统一


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 383 ::
收藏到网摘: n/a

在WEB开发过程中,前后端要求数据编码一致的处理是经常会碰到的! 在非英文字符集的页面上,如果使用Ajax方式进行数据交互的话,就必须要注意保证前后端数据的统一编码,否则,很容易就出现乱码!
在后端是ASP程序的情况下,保持前端Javascript和Asp之间传值的统一编码可以使用以下函数进行处理:
编码:escape(string)
解码:unescape(string)
这两个函数在JavaScript和Asp里都存在,而且功能完全一样,只要任何一端向另一端传数据,都先用escape进行编码,接收的一端用unescape进行解码即可保证Javascript和Asp数据传递过程中不会出现乱码!
在后端是PHP程序的情况下,保持前端Javascript和PHP之间传值的统一编码可以使用以下函数进行处理:
WEB前端JavaScript
编码:encodeURI(string)
解码:decodeURI(string)
WEB后端Php
编码:urlencode(string)
解码:urldecode(string)
同样,传值的时候使用相应的编码函数encodeURI或urlencode,接收的时候使用相应的解码函数decodeURI或urldecode,即可保证Javascript和Php数据传递过程中不会出现乱码!
在WEB前端Javascript解码,Php里urlencode编码的数据时,可能需要使用两次解码,如:
unescape(decodeURI(string))
unescape用来解一些非字符类的文字,如标点符号等等。