How to get Response data character set in Ajax?

Ajax Request:

$("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 ?

Answers 1

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

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

Related Articles