title: jQuery: UTF-8 not working with ajax request
body: <p>I have a php file which uses <code>jQuery.ajax()</code> to grab some data from another php file into a div.</p>
  
  <pre><code>jQuery.ajax({
      type: 'POST',
      encoding:"UTF-8",
      dataType:"html", 
      contentType: "text/plain; charset=UTF-8",
      url: '/path/data.php',
      success: function(msg) {
          jQuery('#dataBox').html(msg);
      }
  });
  </code></pre>
  
  <p>My problem is that if I have some "special" characters in the data I'm getting through ajax such as åäö then I get the "question mark in a black diamond"-mark. If I open the external file in the browser it works. If I put some special characters on the main page, it works.</p>
  
  <p>Some simplified code:</p>
  
  <p>data.php:</p>
  
  <pre><code>$mysqli = new mysqli("localhost", "username", "pass", "db");
  $mysqli-&gt;set_charset("utf8");
  $mysqli-&gt;query("SET GLOBAL time_zone = '+00:00'");
  $stmt = $mysqli -&gt; prepare("SELECT GROUP_CONCAT(sometext) AS mytext FROM `mytable`");
  $stmt -&gt; execute();
  $results = selectResults($stmt);
  $stmt -&gt; close();
  $mysqli -&gt; close();
  </code></pre>
  
  <p>selectResults function:</p>
  
  <pre><code>function selectResults($stmt)
  {
      $parameters = array();
      $results = array();
  
      $meta = $stmt-&gt;result_metadata();
  
      while ( $field = $meta-&gt;fetch_field() ) {   
       $parameters[] = &amp;$row[$field-&gt;name]; 
      }
  
      call_user_func_array(array($stmt, 'bind_result'), $parameters);
  
      while ( $stmt-&gt;fetch() ) {
        $x = array();
        foreach( $row as $key =&gt; $val ) {
           $x[$key] = $val;
        }
        $results[] = $x;
      }
  
      return $results;
  
  }
  </code></pre>
  
  <p>data.php:</p>
  
  <pre><code>foreach($results as $result){
   $textArray = explode(',', $result['mytext']);
  }
  foreach($textArray as $text){
  echo($text);
  }
  </code></pre>