如何在Ajax中获取响应数据字符集?

时间:2022-06-22 20:18:02

Ajax Request:

Ajax请求:

$("button").click(function(){
    $.ajax({url: "demo.html", success: function(result){
        $("#div1").html(result);
    }});
});

Here the ajax request get the data from demo.html file and then result variable contains the response data. So, how to check the character set type of data in result variable. Consider the demo.html is shift_jis. So, while doing ajax request the response data character set is same as demo.html character set or it set as utf-8 ?

在这里,ajax请求从demo中获取数据。html文件和结果变量包含响应数据。因此,如何检查结果变量中的字符集类型。考虑到演示。html是shift_jis。因此,在执行ajax请求时,响应数据字符集与demo相同。html字符集还是utf-8 ?

1 个解决方案

#1


2  

Try to read header using xhr and read charset of header as below

尝试使用xhr读取标题,并阅读下面的标题。

$("button").click(function(){
    $.ajax({url: "demo.html", success: function(response,xhr,result){
    var cs = xhr.getResponseHeader("charset") ;
        $("#div1").html(result);
    }});
});

#1


2  

Try to read header using xhr and read charset of header as below

尝试使用xhr读取标题,并阅读下面的标题。

$("button").click(function(){
    $.ajax({url: "demo.html", success: function(response,xhr,result){
    var cs = xhr.getResponseHeader("charset") ;
        $("#div1").html(result);
    }});
});