ajax: force utf-8 encoding
I have a webpage in UTF-8, defined in head
<meta charset="utf-8" />
Then I have the external .js file, containing the following code:
function ajax_db(){
$(document).ready(function(){
//some variables defined
$.ajax({
type:"POST",
url:"db.php",
data:"u="+uname+"&p="+pass+"&m=1",
dataType:"text",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(reply){
$("#regstat").html("<p class='status'>"+reply+"</p>");
}
});
});
}
As you see, request goes to db.php, and if m set to 1 and not all the fields are filled in, it ends here:
$mode = htmlspecialchars($_POST['m']);
//Mode: 1 - register, 0 - log in, 2 - log out
if ($mode == 1){
if (empty($_POST['u']) || empty($_POST['p']) || empty($_POST['email'])){
$reply = "Словенъска";
echo utf8_encode($reply);
exit;
}
//more code
}
Problem is, it's returning some mojibake instead of text. I tried to force utf-8 encoding wherever I could, I put in db.php开发者_开发知识库 the following lines:
mb_internal_encoding( 'UTF-8' );
header("Content-type: text/html; charset=utf-8");
No effect at all. Furthermore, if I remove the utf8_encode() function, question-mark boxes are returned, which should indicate that no utf-8 is used.
Where could the problem be?
Thanks for your time.
You should also make sure that the db.php file is saved with UTF-8 encoding.
精彩评论