2

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"
});

".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é";

Here is the signature of the connexionAjaxError callback function :

function connexionAjaxError(jqXHR, textStatus, errorThrown)

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.

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.

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/

2 Answers 2

1

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:

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

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.

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

Let me know!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. Now I have got a better idea on how a JS/PHP litteral string is encoded. If the ISO-8859-1 encoding applies on my PHP/JS script then the litteral string "été" is encoded [e9] in hexadecimal notation. But if the UTF-8 encoding applies on my PHP/JS script then the litteral string "été" is encoded [c3 a9] in hexadecimal notation. Tell me if I am wrong.
0

Try to set the Contenttype like this in your call, as far as I know jQuery Ajax is always utf-8 if not specified.

$.ajax({
...
    datatype: "text",
    contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
    error: connexionAjaxError,
    success: connexionAjaxSuccess,
    type: "POST",
...
});

Edit: If my answer isn't helpfull, it might help if you check the servers charset configuration, and also post it.

1 Comment

According to the jQuery documentation at : api.jquery.com/jQuery.ajax The contentType setting only applies on data sent to the server. But my problem concerns data received from the server. I have run the mb_internal_encoding function in one of my PHP script. The mb_internal_encoding function has returned "ISO-8859-1". Consequently : [1] the result string "été" is encoded [e9 74 e9] in hexadecimal notation. [2] I have specified "charset=IS0-8859-1" in the header of my HTTP response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.