最近又开始学PHP了, 这里碰到一个问题, php 在处理jquery 的 ajax 传回来的 json数据中, 中文部分为空.

 数据库为 mysql,

 又是php中文编码的问题.


参考截图:

(左图为解决之前, 右图为解决之后)



偶的解决代码:

1, 后端 php   ( ajaxRequest/GetNotifyList.php )

      

 $list=array('ID'=>$list[ID], 'Title'=>iconv( 'gb2312', 'UTF-8' , $list[Title]));  // 如果这一句不加,就会出现左图效果;反之,问题解决。

<?
    //header("Content-Type:text/json;charset=UTF-8");
	require('../Common/config.php');
	
	$sql='SELECT ID,Title  FROM `YOUR_TABLE_NAME` order by createdate desc limit 0, 6';
	
	//echo $sql;
	mysql_select_db($db,$conn) or die("数据库".$db."连接失败!");
	mysql_query("SET NAMES GBK");  
	
	$rs=mysql_query($sql) or die(mysql_error());
	
	/* 
	$result="";
	while($list=mysql_fetch_array($rs)){
		$ID=$list['ID'];
	    $Title=$list['Title'];
		$result=$result.$ID.":".$Title."|";
	}
	 
	if(!empty($result))
	{
		$result=$result."SELECT_success";
	}else
	{
		$result="SELECT_empty";
	}
	echo $result; 
	*/
	
	
	$notifies=array();
	mysql_query("SET NAMES GBK");  
	$rs=mysql_query($sql) or die(mysql_error());

	while($list=mysql_fetch_array($rs)){
		 $list=array('ID'=>$list[ID], 'Title'=>iconv( 'gb2312', 'UTF-8' , $list[Title])); 
		 $notifies[]=$list; 	
	}
	echo json_encode($notifies); 
	
    //print ($notifies);
	//print_r ($notifies);
 
?>

2, web前端

  在 head 之间

    <script src="./javascript/jquery-1.8.3.min.js" type="text/javascript" charset="UTF-8"></script>
      
<script type="text/javascript">
		$(document).ready(function(){
			 
			var htmlobj=$.ajax({
				url:'ajaxRequest/GetNotifyList.php',
				'type':'get',
				dataType:'json', 
				success:function(d){
					$.each(d,function(index,item){  
					   //输出 
					   //alert(index);
					   //alert(item.ID+"哈哈"+item.Title);  
					   $("#lhScroll").append("<li><a style='color: Green;' href='#' val='"+item.ID+"'>" + item.Title + "</a></li>");
					})
				}
			});
			
			 
		});
    </script>

参考文章: http://unixlzx.blog.163.com/blog/static/1027737522011419492160/


(结束)

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 11 小时前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 17 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐