2

我正在使用 jquery 和 ASP,并且有一个 SQL 数据库,我使用函数 $.getJSON() 从中获取了一些西班牙语描述,其中包含“acentos”和“波浪线”(á、é、í、ó、ú、ñ , ETC。)。

Chrome 4 和 FireFox 运行良好。问题在于 IE8:对于一些特定的查询,它挂起而没有返回结果。如果我使用 IE 将所有“ó”替换为“o”,相同的结果集可以完美运行,所以我知道问题出在“acentos”(ó) 上。

我使用以下代码设置 ajax 调用:

$.ajaxSetup({'beforeSend' : function(xhr) {
            if (xhr.overrideMimeType) {
        //FF & Chrome
               xhr.overrideMimeType('text/html; charset=iso-8859-1');
            }else {
               //IE8
               //I tried everything here:
               //xhr = new ActiveXObject("Microsoft.XMLHTTP");
               //var obj = new ActiveXObject("MSXML2.XMLHTTP.3.0");
               //xhr = new ActiveXObject("Msxml2.XMLHTTP");

               //and get ERROR with IE8 in this line:
               xhr.setRequestHeader('Content-type', 'text/html; charset=iso-8859-1');
            }
                                          }
});

然后,调用 getJSON() 来获取带有“acentos”的描述是这样的:

function showDialog(idCriterio, codAplica, descTema){

$("#dialog-modal").html("Buscando Datos...");//getting data...

$.getJSON("generarJSONTipos.asp", { idCriterio: idCriterio, codAplica: codAplica}, 

  //callback function
  function(data){
    var textoDialogo;
    textoDialogo= "";

    $("#dialog-modal").html("<span class='tituloOpcion'>"+descTema+"</span><br><br>");

    for(i=0;i<data.length;i++) {
      //tomo el html actual
      textoDialogo = $("#dialog-modal").html();
     //le apendo la descripcion del elemento del array
     $("#dialog-modal").html(textoDialogo + data[i].descripcion + "<br>");
      }//end for
                 }//end callback
     );//end getJSON

$("#dialog-modal").dialog({
   height: 300,
   width:450,
   modal: true
  });

$("#dialog-modal").dialog('open');
}

任何提示将不胜感激。我已经用谷歌搜索了好几天,没有得到这个问题的答案......:P

提前致谢, Ignacio(阿根廷拉普拉塔)

4

1 回答 1

3

答案很简单,但我花了几天时间才发现。在这里分享给遇到同样问题的人。

解决方案是在响应对象 (ASP) 或 PHP 中的 header 函数中指定内容类型:

ASP

Response.ContentType="text/html; charset=iso-8859-1"

PHP

header("text/html; charset=iso-8859-1");

这必须是响应的第一行(我认为),在生成 JSON 对象的文件上。任何意见将不胜感激。

谢谢!

伊格纳西奥(阿根廷拉普拉塔)

于 2010-04-16T14:12:13.897 回答