开发者

JQuery problem with HTML entities

I use this code to receive data from an php file

$.ajax({
                url: "includes/cpf.inc.php",
                type: "POST",
                data:{p_request:"1bdd249a5d673a721be31d7444af81af1af4c5b6",
                        p_userid:"<?php echo $dbu_userid; ?>",
                        p_curpass:$("#txtcurpass").val(),
                        p_newpass:$("#txtnewpass").val(),
                        p_cnewpass:$("#txtcnewpass").val()
                },
                cache:false,
                success:function(msg){
                    $("#chPassLoad").css("visibility","hidden");
                    wilPrompt("#chpasStat",msg.substr(0,1),msg.substr(2));
                    if(msg.substr(0,1)=="0"){
                        $("#txtcurpass").attr("value","");
                        $("#txtnewpass").attr("value","");
                        $("#txtcnewpass").attr("value","");
                    }
                },
                beforeSend:function(){
                    $("#chPassLoad").css("visibility","visible");
                }
            });

and the data that will be receive from cpf.inc.php contains html entities like this

&#12362;&#12399;&#12424;&#12358;&#12372;&#12374;&#12356;&#12414;&#12377;&#28450;&#23383

and that html entities are Japanese characters

おはようござい开发者_高级运维ます漢字

now my problem is when I will receive data from the variable msg it returns something like this

pX[h&sigmaf;&#1794;

Is there any code I need to append on the $.ajax parameters to encode this html entities correctly? thanks


I am guessing that you need to set the charset for the request.. here is how you can do it:

$.ajaxSetup({
    'beforeSend' : function(xhr) {
        xhr.overrideMimeType('text/html; charset=UTF-8');   //set the right charset here
    },
});

or

$.ajax({
                url: "includes/cpf.inc.php",
                type: "POST",
                data:{p_request:"1bdd249a5d673a721be31d7444af81af1af4c5b6",
                        p_userid:"<?php echo $dbu_userid; ?>",
                        p_curpass:$("#txtcurpass").val(),
                        p_newpass:$("#txtnewpass").val(),
                        p_cnewpass:$("#txtcnewpass").val()
                },
                cache:false,
                success:function(msg){
                    $("#chPassLoad").css("visibility","hidden");
                    wilPrompt("#chpasStat",msg.substr(0,1),msg.substr(2));
                    if(msg.substr(0,1)=="0"){
                        $("#txtcurpass").attr("value","");
                        $("#txtnewpass").attr("value","");
                        $("#txtcnewpass").attr("value","");
                    }
                },
                beforeSend:function(xhr) {
                         xhr.overrideMimeType('text/html; charset=UTF-8');
                         $("#chPassLoad").css("visibility","visible");
                         }
       });


I faced same problem using PHP base64_encode($str) to pass XML code to jquery and decode the string with jQuery.base64. JPN fonts get problem / Chinese fonts is normal.

Use htmlentities($str,,'utf-8') instead of base64_encode is OK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜