jQuery的AJAX +的responseText +字符编码字符、jQuery、AJAX、responseText

2023-09-10 21:14:36 作者:嫉妒我帅倪报警

我执行一个Ajax请求的Jav​​aScript和使用jQuery库的PHP脚本。 这是我的命令,该命令执行AJAX请求:

I perform an AJAX request to a PHP script in JavaScript and by using the jQuery library. Here is my command which performs the AJAX request :

$.ajax({
    async: "false",
    cache: "false",
    data: {login: strLogin, password: strPassword},
    datatype: "text",
    error: connexionAjaxError,
    success: connexionAjaxSuccess,
    type: "POST",
    url: "./Connexion"
});

联接是一个URL重定向到我的PHP脚本。 connexionAjaxError是不可避免一旦接收到HTTP响应执行的JavaScript功能。 原因:我的PHP脚本结果为纯文本/页,HTTP状态code等于500 这是我的PHP脚本的最相关的部分:

".Connexion" is a URL redirection to my PHP script. connexionAjaxError is a JavaScript function inevitably executed as soon as the HTTP response is received. The reason : My PHP script results to a text/plain page with HTTP status code equal to 500. Here is the most relevant part of my PHP script :

header("HTTP/1.0 500 Internal Server Error; 
        Content-type: text/plain; 
        charset=ISO-8859-1");

echo "été";

下面是connexionAjaxError回调函数的签名:

Here is the signature of the connexionAjaxError callback function :

function connexionAjaxError(jqXHR, textStatus, errorThrown)

我的PHP结果字符串ETE。 我的PHP内部编码为ISO-8859-1这样的结果字符串连接十六进制的codeD [E9 74 E9。

My PHP results to the string "été". My PHP internal encoding is ISO-8859-1 so the result string is encoded [e9 74 e9] in hexadecimal notation.

但jqXHR.responseText在connexionAjaxError回调函数包含1个字符的字符串。 我已经使用了JavaScript的String对象的字符$ C $猫的方法来获得UNI code(UTF-8?)code表示的字符。 该角色是连接在十进制形式相当于codeD 65535 [FF FF]以十六进制表示。

But jqXHR.responseText in the connexionAjaxError callback function contains a 1-character string. I have used the charCodeAt method of the JavaScript String object to get the unicode (UTF-8 ?) code for the character. The character is encoded 65535 in decimal notation equivalent to [ff ff] in hexadecimal notation.

我不明白为什么jqXHR.responseText不包含3个字符的字符串端到端。 我不是在JavaScript方面的专家,但我想jqXHR.responseText只能包含一个字符串连接codeD的UTF-8。 因此,在jqXHR.responseText字符串ETE将连接以十六进制格式codeD [c3a9 74 c3a9。 我检索UTF-8 code从以下网站上的字符e: HTTP:/ /www.utf8-chartable.de/

I do not understand why jqXHR.responseText does not contain the 3-character string "été". I am not an expert in JavaScript but I suppose jqXHR.responseText can only contains a string encoded in UTF-8. So the string "été" in jqXHR.responseText would be encoded [c3a9 74 c3a9] in hexadecimal notation. I have retrieve the UTF-8 code for the character "é" from the following web site : http://www.utf8-chartable.de/

推荐答案

我做了很多发展的西班牙语和我注意到一些东西,可以帮助你,因为我已经通过这个已经走了:

I make a lot of development in Spanish and I've noticed a few things that might help you, since I've gone through this already:

在头功能的字符集应该是UTF-8。

The charset in the header function should be UTF-8.

另外,文件本身(PHP脚本)必须保存在UTF-8,你可以很容易地做到这一点,如果你编辑与记事本++的文件,并选择编码为UTF-8无BOM。

Also, the FILE itself (the PHP script) must be saved in UTF-8, you can easily achieve this if you edit the file with Notepad++ and select the encoding to be UTF-8 without BOM.

也是一样的JavaScript和HTML文件,他们都应该是连接codeD使用UTF-8无BOM,而不是ANSI。

Same goes for your javascript and HTML files, they should all be encoded with UTF-8 without BOM rather than ANSI.

让我知道!