jquery / ajax设置charset标头

时间:2010-07-04 15:08:31

标签: javascript jquery ajax

我正在尝试在jquery调用中设置charSet,我正在制作的网站是立陶宛朋友,因此有一些带有重音符号的字母等。

至于我迄今为止的研究(2天值得!!!)已经显示,我需要将它放在beforeSend部分,我已经做了如下:

     $(document).ready(function(){
 $('.content').load('home.html');   //by default initally load text from boo.php
     $('#navlist a').click(function() { //start function when any link is clicked
                    $(".content").slideUp("slow");
                     var content_show = $(this).attr("title"); //retrieve title of link so we can compare with php file
                        $.ajax({
                        method: "load",url: ""+content_show,
                        beforeSend: function(){
                            XMLHttpRequest.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
                            $("#loading").show("fast");
                        }, //show loading just when link is clicked
                        complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
                        success: function(html){ //so, if data is retrieved, store it in html
                        $(".content").show("slow"); //animation
                        $(".content").html(html); //show the html inside .content div
                 }
             }); //close $.ajax(
     }); //close click(
 }); //close $(

我尝试将其更改为

setRequestHeader("Content-Type", "text/plain;charset=UTF-8");

哪个也行不通,所以现在我被困住了,在我看了四天的时候,我的神经紧张。

该网站可以在43dennis.co.nr/bivakas查看。基本上它加载了一堆方形黑色问号代替带有特殊重音的字母。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:5)

由于安全限制,这不起作用。您需要在此JS运行的父页面中设置charset。 JS / jQuery将使用相同的字符集。

我检查了您网页的HTML,我看到以下内容:

<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

您需要将iso-8859-1替换为UTF-8,然后您不需要在JS / jQuery中使用它。

我检查了您网页的HTTP响应标题,我看到以下内容:

Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sun, 04 Jul 2010 13:37:08 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: PHP/5.2.10
Content-Type: text/html
Content-Length: 600

200 OK

Content-Type缺少charset属性。您还需要在HTTP响应标头中设置相同的内容。把它放在你的PHP代码中,之前将任何字符发送到响应体中。

header('Content-Type: text/html; charset=UTF-8');
相关问题