Imran Ahmed
Imran Ahmed

Reputation: 800

$.ajax and accented characters issue

I am using this ajax code to send data to server:

$.ajax({
    data: postData,
    type: method,
    url: url,
    timeout: 20000,
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    error: function(jqXHR,textStatus,err){alert("Error returned from ajax call "+err);},
    success: function(data,status,jqXHR){
        // process response...
    }
});

postData is a query string with many values while method is GET or POST

The problem is that, when I send a query string that contains a value like Älypuhelimen lisävarusteet, the result in database is �lypuhelimen lis�varusteet

The database connection collation is utf-8, and this works fine when I do not use ajax to post and save to database... It is definitely ajax that messes up the encoding...

I have also tried using encodeURIComponent() function on the data, and it becomes %C4lypuhelimen%20lis%E4varusteet if I use it... same goes for escape() function...

any help will be appreciated...

Upvotes: 1

Views: 828

Answers (3)

Imran Ahmed
Imran Ahmed

Reputation: 800

Just for information and to help others that might fall in the same situation...

The problem was with the postData itself... it was parsed such that every post variable was applied with escape()... Using encodeURIComponent() instead of escape() worked!

Summary: Donot use escape() function to url-escape query components... use encodeURIComponent() instead...

Upvotes: 0

Aruna Warnasooriya
Aruna Warnasooriya

Reputation: 339

You should try jQuery Base64 encode.

JavaScript:

<script src="jquery.min.js"></script>
<script src="jquery.base64.min.js"></script>
<script>
    enctext = $.base64.encode("yourtext");
    //your ajax code goes here.
</script>

PHP :

<?php
   $org_text = base64_decode($_POST['your_variable']);
?>

jQuery Base64 plugin.

download from here.

https://github.com/carlo/jquery-base64

Upvotes: 1

Alisson Linneker
Alisson Linneker

Reputation: 292

Try changing the column in the database to utf16_bin Collation

Post you php database connection code.

Upvotes: 0

Related Questions