PHP中ajax的数据乱码问题

WBOY
Release: 2016-06-23 13:30:47
Original
805 people have browsed it

今天试了下简单的ajax测试
然后出现了乱码

<?phpheader("Content-Type: text/html; charset=utf-8");class C_test2 extends CI_Controller{    public function __construct()    {        parent::__construct();    }    public function test(){        $data=$_POST['id'];               echo 'id的值为'.$data;    }}?>
Copy after login

<!DOCTYPE html><html><head><meta charset="utf-8"><title>Insert title here</title><script src="js/jquery.js" type="text/javascript"></script><script src="js/test.js"></script><link href="css/test.css" rel="stylesheet" type="text/css"></head><body><input type="text" id="wenben"></input><input type="submit" id="tijiao"></input></body></html>
Copy after login

$(function(){	$("#tijiao").click(function(){		var turl="http://localhost/ajaxCI/C_test2/test";		var tval=$("#wenben").val();				$.ajax({			type:"POST",			url:turl,			data:{id:tval},			dataType:"html",			async:false,			success:function(back){				alert(back);			}					});	})})
Copy after login




然后我把PHP那个header头的编码改为GBK时是这样的



我擦,这是二选一的节奏么,为何不统一。。。


回复讨论(解决方案)

虽然你在 php 文件中声明了 utf-8
但是你的程序文件却是 gbk 的

那应该怎么解决?

两边得同步,要gbk都是gbk,要utf8都是utf8……

把程序文件保存为 无BOM头的utf-8 格式

或者
echo iconv('gbk', 'utf-8', 'id的值为') . $data;

把文件编码改为UTF-8就搞定了

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template