Stuck at Jquery Ajax Db Encoding for ISO-8859-9 Ch

2019-08-31 12:46发布

问题:

I am trying to pass data to db via my Jquery application. I have serious problems with encoding.

Current page encoding is iso-8859-9. I've made my ajax page encoding to iso-8859-9 and passing .ajax data with contentType: "application/x-www-form-urlencoded;charset=ISO-8859-9"

It stores İZİN value as Ä°ZÄ°N with every single trying.

Is it work if I create a PHP page and set encoding with headers?

PS: I am processing data on an HTML page. Processor page is an ASP page and using MsSQL db I guess. I don't have access to processor page or db. Just entering data via form post.

İZİN word and some other similar words are using for sql query at some other pages on WHERE clauses. So I cannot convert characters into HTML codes. I have to use İ as İ.

Thanks for suggestions!

回答1:

The application/x-www-form-urlencoded does not have a charset. It's simply ASCII characters. Specifying charset there will do nothing.

jQuery will normally urlencode your data as is specified:

  1. Encode to UTF-8
  2. Percent-encode

So:

$.post( "test.php", {data: 'İZİN'}); //Shorthand for $.ajax

Actually posts this to server:

data=%C4%B0Z%C4%B0N

When you access $_POST['data'] with php, they have been turned into bytes (0xC4B05AC4B04E), so echoing them will give you malformed data:

header("Content-Type: text/html; charset=ISO-8859-9");
echo $_POST['data'];
// Ä°ZÄ°N

You can test this is true with:

header("Content-Type: text/html; charset=ISO-8859-9");
echo "\xC4\xB0\x5A\xC4\xB0\x4E";
// Ä°ZÄ°N

In PHP you need convert it to ISO-8859-9 as soon as you receive it:

<?php
header("Content-Type: text/html; charset=ISO-8859-9");
$data =  "\xC4\xB0\x5A\xC4\xB0\x4E"; //$_POST['data'];
$proper = mb_convert_encoding( $data, "ISO-8859-9", "UTF-8" );
echo $proper;
//İZİN

Note that it's just much easier to use UTF-8 everywhere because it's pretty much the best encoding and the web loves it. If you use any other encoding then you will have to be on your toes all the time.



回答2:

Sounds like the problem is accessor page does not support utf-8.
I'm not foreign to word İZİN and its gibberish version Ä°ZÄ°N. Most likely it is due to codepage.

From JQuery.ajax()

POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

So, in your ajax settings, charset definition ignored.

You need to access to page which you called as processor page. It must have utf-8 codepage (65001).

As temporarily on client side, you can replace letters encoded in UTF-8 as two bytes with expected single byte form before sending data.

function tr_map(data){
    var tr_data = data;
    var chrmap = {
        "%C3%B6" : "%F6", //ö
        "%C3%A7" : "%E7", //ç
        "%C5%9F" : "%FE", //ş
        "%C4%B1" : "%FD", //ı
        "%C4%9F" : "%F0", //ğ
        "%C3%BC" : "%FC", //ü
        "%C3%96" : "%D6", //Ö
        "%C3%87" : "%C7", //Ç
        "%C5%9E" : "%DE", //Ş
        "%C4%B0" : "%DD", //İ
        "%C4%9E" : "%D0", //Ğ
        "%C3%9C" : "%DC"  //Ü
    };
    for (var chr in chrmap)
        if(chrmap.hasOwnProperty(chr))
            tr_data = tr_data.split(chr).join(chrmap[chr]);
    return tr_data;
}

$.ajax({
    url: "...",
    type: "POST",
    data: $("#myform").serialize(),
    /*
    other settings
    success : ...
    */
    beforeSend: function(x, s){s.data = tr_map(s.data);}
});