jquery ajax交付中文出现乱码
Jun 13, 2016 pm 01:24 PM
gt
html
mysql
pdo
quot
jquery ajax提交中文出现乱码?
提交页面为submit.htm代码如下
- HTML code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>欢迎</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> ... $.ajax({ type:"POST", url: "xx/add_post.php", data:'aa='+escape($('#aa').val())+'&bb='+$('#bb').val(), success: function(html){ alert('添加成功!'); } }); ....
Copy after login
接收数据页为add_post.php
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php header("Content-Type:text/html;charset=utf-8"); date_default_timezone_set("PRC"); try{ $pdo=new PDO("mysql:host=localhost;dbname=acom", "root", "abcdefghi"); }catch(PDOException $e){ echo $e->getMessage(); } $stmt=$pdo->prepare("insert into acom_a(aa, bb) values(:aa, :bb)"); $stmt->execute(array(":aa"=>$_POST["aa"], ":bb"=>$_POST["bb"])); ?>
Copy after login
aa字段存入中文为乱码,bb是英文正常!
------解决方案--------------------
单独执行 add_post.php 文件插入中文乱码吗?
------解决方案--------------------
encodeURI($('#bb').val()); 这样,把可能带中文的变量都encodeURI
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
问题解决了就好!
在实例化 PDO 时附加的参数 array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8")
表示的意思是:连接mysql成果后,立即执行 set names utf8 指令,即将通讯用的字符集设为 utf-8
由于 mysql 数据库都是独立的,公共的。所以打不大可能就只按你需要的参数被安装
所以使用时声明一下字符集是很有必要的
mysql 对于 PDO 就属于个性化的东西了。对于声明字符集,他需要执行 sql 指令,要是 oralce 就是在 dsn 中声明了
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to fix mysql_native_password not loaded errors on MySQL 8.4

How do you parse and process HTML/XML in PHP?
