jQuery AJAX Character Encoding

JqueryAjaxCharacter Encoding

Jquery Problem Overview


I'm currently coding a French website. There's a schedule page, where a link on the side can be used to load another day's schedule.

Here's the JS I'm using to do this:

	<script type="text/javascript">
	function load(y) {
		$.get(y,function(d) {
			$("#replace").html(d);
			mod();
		});
	}
	function mod() {
		$("#dates a").click(function() {
			y = $(this).attr("href");
			load(y);
			return false;
		});
	}
	mod();
	</script>

The actual AJAX works like a charm. My problem lies with the response to the request.

Because it is a French website, there are many accented letters. I'm using the ISO-8859-15 charset for that very reason. However, in the response to my AJAX request, the accents are becoming ?'s because the character encoding seems to be changed back to UTF-8.

How do I avoid this? I've already tried adding some PHP at the top of the requested documents to set the character set:

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>

But that doesn't seem to work either. Any thoughts?

Also, while any of you are looking here...why does the rightmost column seem to become smaller when a new page is loaded, causing the table to distort and each <li> within the <td> to wrap to the next line?

Cheers

Jquery Solutions


Solution 1 - Jquery

Specifying the content type on the AJAX-call solved my problems on a Norwegian site.

$.ajax({
        data: parameters,
        type: "POST",
        url: ajax_url,
        timeout: 20000,
        contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
        dataType: 'json',
        success: callback
});

You would also have to specify the charset on the server.

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>

Solution 2 - Jquery

UTF-8 is supposed to handle all accents and foreign chars - why not use it on your data source?

EDIT
[Archive copy of the test file.] with your data

Everything should be UTF-8 in the first place. I loaded the files in notepad++, converted to utf-8 and manually changed the charactes to accents were needed. Once done everything's working like a charm.

BTW, unless your server is defined to php-process .html files, the files you're loading with ajax aren't getting your iso charset. If you insist on using the iso charset, request a php file instead of an html file, and define the charset in the header (not in the file itself)

Solution 3 - Jquery

$str=iconv("windows-1250","UTF-8",$str);

what helped me on the eventually

Solution 4 - Jquery

You need to set up your server to use ISO-8859-15 as the character encoding (adding the appropriate HTTP header). Doing it in the body of the html won't help.

I can see this line

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>

at the source of your html. This shouldn't happen. Using Live HTTP Headers I can't see the appropriate charset HTTP header. Use that for both your first page and the ajax service.

Solution 5 - Jquery

I would strongl suggest the use of the javascript escape() method

you can use this with jQuery by grabbing a form value like so:

var encodedString = escape($("#myFormFieldID").val());

Solution 6 - Jquery

For my PHP application the solution was include utf8_decode function for each variable get in $_POST.

Ex: (SERVER SIDE)

if( strtoupper($_SERVER['REQUEST_METHOD']) == "POST" ){
$variable = htmlentities($_POST['variable']); // wrong
$variable = htmlentities( utf8_decode($_POST['variable']) ); // right
}

Abraços!!! (Bye Bye!!!)

Solution 7 - Jquery

When printing out the variables in the ajax file. Put a

htmlentities()

Around them, see if it works. Worked for me in an Icelandic ajax application.

Solution 8 - Jquery

I was having similar problems, working in a content comments system in our Spanish Portal. What finally solved my problem, after many hours of searching, instead of messing with jQuery charset, that seems to use utf-8 no matter what, it was to decode from utf-8 back to ISO-8859-1 in the PHP that processed the ajax POST. In PHP there is a built in function, utf8_decode(), so the first thing I do with the comments string is this:

$comentario = utf8_decode($_POST['comentario']);

(and then I used nl2br() and htmlentities() PHP functions in order to prepare the text to be stored with html entities instead of special chars)

Good Luck & Peace all over! Seba

Solution 9 - Jquery

I had an issue with Swedish/Norwegian letters showing up as question marks, despite having specified:

contentType: "application/json; charset=utf-8",

This was solved by adding encodeURIComponent to the string.

url: "/set_comment.do?text=" + encodeURIComponent(comment),

Solution 10 - Jquery

I have been fiddling around with this problem and found out that this solution works for Firefox and safari (yes, im on a mac at the moment).

when getting the request, i made a content-type=iso-8859-1 here:

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml; charset=ISO-8859-1');
        }
    }

Please tell me if someone finds out this doesn't work in ie.

Solution 11 - Jquery

I had a similar problem with ASP with my pages. I've resolved so:

  1. Inserted at the begin of the ASP file called by Jquery ajax method the instruction:

    Response.Charset = "windows-1252"

In My asp page before the charset wasn't defined and i had saved my file with ANSI encoding (with a text editor as notepad++)

  1. In the html file that calls the ASP file with the jquery ajax method i've inserted the parameter content-type that you see below:

    $.ajax({
    data: parameters,
    type: "POST",
    url: ajax_url, datatype: 'html',
    contentType: "text/html;charset=windows-1252",

Briefly, it's important to define in your ASP file called the charset. Strangely, I see that my jquery works with windows-1252 but doesn't work with ISO-8859-1 charset: in this case jquery ajax method give an exception.

Solution 12 - Jquery

I tried many suggestions to read in a textfile with German special characters (ä,ö,ü). In the end:

 $.ajax({
        async:false,
        type: "GET",
        url: "data/FileName.txt",
        dataType: "text",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        success: function (data) {
           alert(data);
        }
    });

let me read in the special characters, but only AFTER I explicitly saved FileName.txt in the UTF-8 format. The standard format for saving text files in the Windows Editor is ANSI and not UTF-8, but it can be changed if you "Save as" and use the dropbox next to the Save-Button or use a better Editor to start with.

Solution 13 - Jquery

I DONT AGREE everything must be UTF-8, you can make it work perfectly with ISO 8859, I did, please read my response here.

my response in stackoverflow

Solution 14 - Jquery

<script type="text/javascript">
  function GetContent(idUser){ 
    $.ajaxSetup({
      url: 'ping.php'
    });

	$.ajax({
	  type: "POST",
	  url: "blablabla.php",
	  dataType: "text",
	  data: ({ParamUserID:idUser}),// here we def wich variabe is assiciated
	  contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
	  success: function(html) {
		$('#user_specified_content').html(html);
	  }
	});		
  }
</script> 

This is the script from which I get my content from the blabla.php page.

When I receive my data on success my accents don't appear.

I have found a super good working solution by my own for fetching data with accents on the fetched data from .success function

On my blabla page. I did this to bring a solution to my problem:

I use utf8_encode(place your data here) in my php file. See it in action below:

while( $row = sqlsrv_fetch_array($recordset) )
{							
    <option value="<?php echo $row["xyz"]; ?>"><?php echo utf8_encode($row["gName"]) . ' | ' . utf8_encode($row["gManagerName"]); ?></option>							
}

Solution 15 - Jquery

I have tried to read a local json/text file using $.ajax and the encoding didn't work at first.. I got it working by saving the json file as UTF-8 encoding. This can be done simply by windows notepad.. use "save as", and then below the file name you can set the encoding to UTF-8.

good luck!

Solution 16 - Jquery

I´ve had the same problem with pages that:

  • Show up fine when called normally
  • Have the special characters messed up when called via an ajax request

To solve the problem (using php), I used utf8_encode() or htmlentities() on the source data. Both worked, I have used them in different projects.

Solution 17 - Jquery

Just changed {"type":"GET"} to {"type":"POST"} and worked like a charm with ISO-8859-1

Solution 18 - Jquery

If the whole application is using ISO-8859-1, you can modify your jquery.js replacing all occurences of encodeURIComponent by escape (there are 2 occurrences to replace in the current jquery script - 1.5.1)

See encodeUIComponent and escape for more information

Solution 19 - Jquery

I have faced same problem and tried several ways. And i found the solution. If you set request header for "Accept" like below;

$.ajax({
        data: parameters,
        type: "POST",
        url: ajax_url,
        dataType: 'json',
        beforeSend : function(xhr) {
            xhr.setRequestHeader('Accept', "text/html; charset=utf-8");
        },
        success: callback
});

you will see that all the characters seems correct

Solution 20 - Jquery

I came by this question with exactly the same issue as the asker. The answers here are very informing, and I tried out almost every single one of the suggested answers. None of the answers here worked for me though - there might have been a slight difference in my problem. And that seems to be the case for many. I tried so much before ending up with an answer.

So now I'm adding to the already long list of answers, a solution of my own that solved my kind of encoding problem.

I did just two things. In the database connection script I added:

mysql_set_charset('utf8',$connection);

and apart from that:

  • I changed the encoding of all the files to UTF-8

The point is that everything must be of the same encoding. And when creating new php-files, for the scripts it is not enough just to change the encoding in the script - the file itself must be correctly encoded.

Solution 21 - Jquery

If you are using CodeIgniter you can solve this by adding the following code to your Controller before loading any Views (assuming you have charsetproperly set on your config. If not, just put charset=whateveryouwant.

$this->output->set_header('Content-type: text/html; charset='.$this->config->item('charset'));

The way I did it was to add that line to the constructor of MY_Controller, my superclass for all Controllers, this way I make sure I will have no encoding problems anywhere.

By the way, this doesn't affect JSON returns (which are encoded in UTF-8).

Solution 22 - Jquery

I had similar problem. I have text file with json data that has French text. There was always issue displaying some characters. In my case, JavaScript program uses Ajax to retrieve the json text file as follows:

$.ajax({
	async: false,
    type: 'GET',
	url: 'some-url',
    success: function(data, status) {
		mainController.constructionStageMaster = data.records;
	 }
});

The returned data always had incorrect accented French letters.

The json text looks as follows:

{
	"records": [
		{
			"StageDesc": "Excavation, Fondation et Bases",
			"Allowed": 9,
			"Completed": 0,
			"TotalCompleted": ""
		},
		{
			"StageDesc": "Étanchement et Remblayage",
			"Allowed": 3,
			"Completed": 0,
			"TotalCompleted": ""
		},
		{
			"StageDesc": "Encadrement et revêtement mural intermédiaire",
			"StageDesc_fr": "Encadrement et revêtement mural intermédiaire np++",
			"StageDesc_fr2": "Encadrement et revêtement mural intermédiaire",
			"Allowed": 15,
			"Completed": 0,
			"TotalCompleted": ""
		}
        ...
    ]
}

Note in the above sample data, the 3rd element, I placed the text with the correct é and incorrect ê French accented letters.

For your information, it seemed that there is some global configuration of Eclipse project using ISO-8859-1 character encoding. In your case it might be different encoding.

After checking the solutions above and playing around with the the project, this is what solved my problem:

  • Find the source of the text which has the correct display of the French text
  • Copy the text
  • Goto Eclipse, open the text file which has the data
  • Right-click the file in the explorer, open properties, and change the encoding to ISO-8859-1
  • Fix the text so that it is displayed properly. Use copy/paste or the keyboard
  • You may have to use the keyboard arrow keys (left/right) to make sure there are no hidden culprit letters that will show-up on HTML with a funny shape. Delete such hidden letter
  • Save the file

Now, in my case, I didn't specify any encoding options in the Ajax call and it works fine. Also, if I change the encoding of the text file with json data, it would still work fine.

Tarek

Solution 23 - Jquery

jQuery by default assumes UTF-8, to change this default do:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script>
  $.ajaxSettings.mimeType="*/*; charset=ISO-8859-15";// set you charset
</script>

This works fine for me!... ;))...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaltyView Question on Stackoverflow
Solution 1 - JqueryMagnarView Answer on Stackoverflow
Solution 2 - JqueryyoavfView Answer on Stackoverflow
Solution 3 - JqueryanonyView Answer on Stackoverflow
Solution 4 - JquerykgiannakakisView Answer on Stackoverflow
Solution 5 - JqueryDougView Answer on Stackoverflow
Solution 6 - JqueryRafael - orafaelreisView Answer on Stackoverflow
Solution 7 - JqueryÓlafur WaageView Answer on Stackoverflow
Solution 8 - JquerySebastian AlberoniView Answer on Stackoverflow
Solution 9 - JqueryynglingView Answer on Stackoverflow
Solution 10 - JqueryKyussView Answer on Stackoverflow
Solution 11 - JqueryRobView Answer on Stackoverflow
Solution 12 - JqueryjankView Answer on Stackoverflow
Solution 13 - JqueryRodrigo AsensioView Answer on Stackoverflow
Solution 14 - Jqueryclassics40View Answer on Stackoverflow
Solution 15 - JquerydoronView Answer on Stackoverflow
Solution 16 - JqueryjeroenView Answer on Stackoverflow
Solution 17 - JqueryMauroPorrasView Answer on Stackoverflow
Solution 18 - JqueryJonathan PasquierView Answer on Stackoverflow
Solution 19 - JqueryHüseyin BABALView Answer on Stackoverflow
Solution 20 - JquerySteevenView Answer on Stackoverflow
Solution 21 - JqueryChristian DecheryView Answer on Stackoverflow
Solution 22 - JquerytarekahfView Answer on Stackoverflow
Solution 23 - JqueryfwBasicView Answer on Stackoverflow