Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird encoding with ajax response

I need to send a string with the Ajax function from Jquery to my javascript file. The problem is that i get a set of weird questionmarks/diamondblocks before the string i need to receive.

         (function( $ ) 
 {
  $.fn.CallPhpClass = function(className, functionName, parameters, destination)
  {
      return this.each(function()
      {
        if (destination === undefined) {
            destination = $(this);
        };

        $.ajax({
        type : 'POST',
        url : 'php/executer.php',
        data : { className : className, functionName : functionName , parameters : parameters},
        dataType:'text',
        cache: false,
        success : function(data) {
            if (className == "User" && functionName == "logout")
            {
                getMenu();
                return;
            }
            if (className == "User" && functionName == "login")
            {
                getMenu();
                return;
            }
            if (className == "User" && functionName == "lastView")
            {
                $('#main-content').CallPhpClass(data, 'view');
                return;
            }
            if (data.search('alert alert-error') != -1 || data.search('alert alert-success') != -1)
            {
                $('#main-content').CallPhpClass('User', 'lastView');
                destination = $('#error-box');
            }

            if(destination != false)
            {
                destination.html(data);
            }

        },
        error : function(data) {
            console.info(data);
        }
    });
});

}
})

The piece of code that sets the class is:

            if (className == "User" && functionName == "lastView")
            {
                $('#main-content').CallPhpClass(data, 'view');
                return;
            }

With firebug i receive this:

firebug view

All charsets are on UTF-8 and i dont know what i do wrong!

Can someone help me?

Kind regards,

like image 518
Silverwind91 Avatar asked May 03 '26 12:05

Silverwind91


1 Answers

Check the meta tag's characterset

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

set the executer.php file's response's header

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

And while sending Ajax request use contentType and pass charset

$.ajax({
        type : 'POST',
        url : 'php/executer.php',
        data : { className : className, functionName : functionName , parameters : parameters},
        dataType:'text',
        cache: false,
        contentType: "application/json; charset=utf-8",
        success : function(data) {

If it still doesn't works then try charset in your <script> tag while including the js file

<script type="text/javascript" src="myscripts.js" charset="UTF-8"></script> 
like image 89
Imdad Avatar answered May 05 '26 00:05

Imdad