weixin_33737134 2011-05-06 06:18 采纳率: 0%
浏览 296

ajax:强制utf-8编码

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.

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2011-05-06 17:54
    关注

    You should also make sure that the db.php file is saved with UTF-8 encoding.

    评论

报告相同问题?