在JSON响应中的特殊字符(E) [英] Special characters (ë) in JSON-response

查看:245
本文介绍了在JSON响应中的特殊字符(E)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库存储一些文本,我必须得到使用AJAX。这是怎么回事很好,但只有当它不包含特殊字符,如e,埃。我发现这个课题叫我改变了AJAX请求的字符集的一些文章,但这些都不适合我。

当我启动萤火它说这一下头:

Antwoordheaders(荷兰语为responseHeaders响应)
缓冲控制无店面,无缓存,必重新验证,后检查= 0,pre-检查= 0
连接关闭
内容长度94
内容类型text / html;字符集= ISO-8859-15
日期星期三,2012年9月26日九时52分56秒GMT
过期周四,1981年11月19日8时52分00秒GMT
语用无缓存
服务器Apache的
的X技术,通过PleskLin

Verzoe​​kheaders(荷兰语为requestheaders)
接受text / html的,是application / xhtml + xml的,应用程序/ XML; Q = 0.9, / 的; Q = 0.8
接受编码gzip压缩,紧缩
接受语言NL,EN-US; Q = 0.7,连接; Q = 0.3
授权基本c3BvdGlkczp6SkBVajRrcw ==
连接保持
内容类型text / html;字符集= ISO-8859-15
曲奇__utma = 196329838.697518114.1346065716.1346065716.1346065716.1; __utmz = 196329838.1346065716.1.1.utmcsr =(直接)| utmccn =(直接)| utmcmd =(无); PHPSESSID = 2h4vu8gu9v8fe5l1t3ad5agp86
DNT 1
主机www.spotids.com
Referer的 http://www.spotids.com/private/?p=16 < BR> 用户代理的Mozilla / 5.0(Windows NT的6.1; WOW64; RV:14.0)的Gecko / 20100101 Firefox的/ 14.0.1

这两个头都在谈论的charset = ISO-8859-15,其中应包括像电子字符,但它不为我工作。

我用这个(PHP)的code:

`$ newresult =请求mysql_query($ query2);
    $结果=阵列();
    而($行= mysql_fetch_array($ newresult))
    {
        array_push($结果,$行);
    }
    $ jsonText = json_en code($结果);
    回声$ jsonText;`

解决方案
  1. 请确保您设置的标题为UTF-8:

     标题(内容类型:应用程序/ JSON;字符集= UTF-8);
     

  2. 请确保您连接到数据库的任何查询之前进行使用UTF-8编码:

      $查询=请求mysql_query(集名称'UTF8');
     

  3. 据我所知,JSON EN codeS不能再psented纯ASCII $ P $的任何字符。你应该去code表示JSON的响应。

  4. 尝试移动到PDO为 mysql的_ * 函数去precated。使用这个漂亮的教程

My database stores some texts which I have to get with AJAX. This is going well but only when it not contains special characters such as ë or ä. I found some articles about this topic which told me to change the charset of the AJAX-request, but none of these worked for me.

When I start firebug it said this about the headers:

Antwoordheaders (dutch for responseheaders)
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection close
Content-Length 94
Content-Type text/html; charset=ISO-8859-15
Date Wed, 26 Sep 2012 09:52:56 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Pragma no-cache
Server Apache
X-Powered-By PleskLin

Verzoekheaders (dutch for requestheaders)
Accept text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Encoding gzip, deflate
Accept-Language nl,en-us;q=0.7,en;q=0.3
Authorization Basic c3BvdGlkczp6SkBVajRrcw==
Connection keep-alive
Content-Type text/html; charset=ISO-8859-15
Cookie __utma=196329838.697518114.1346065716.1346065716.1346065716.1; __utmz=196329838.1346065716.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); PHPSESSID=2h4vu8gu9v8fe5l1t3ad5agp86
DNT 1
Host www.spotids.com
Referer http://www.spotids.com/private/?p=16
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1

Both of the headers are talking about charset=ISO-8859-15 which should include characters like ë, but it doesn't work for me.

The code I used for this (PHP):

`$newresult = mysql_query($query2);    
    $result = array();
    while( $row = mysql_fetch_array($newresult))
    {
        array_push($result, $row);
    }
    $jsonText = json_encode($result);
    echo $jsonText;`

解决方案

  1. Make sure you set the headers to UTF-8:

    header('Content-Type: application/json; charset=utf-8');
    

  2. Make sure your connection to database is made with UTF-8 encoding before any queries:

    $query = mysql_query("SET NAMES 'UTF8'");
    

  3. As far as I know, JSON encodes any characters that cannot be represented in pure ASCII. And you should decode that JSON on response.

  4. Try to move to PDO as mysql_* functions are deprecated. Use this nice tutorial

这篇关于在JSON响应中的特殊字符(E)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆