0

I'm developing a dashboard which can send certain POST requests to my http server. The POSTs are sent through Ajax and any http errors are being output to the client on a "console" (a modified <textarea>).

Ajax call:

function download() {
  var url = "/myApi/getData/2";
  $.ajax({
    type: "GET",
    url: url,
    data: "",
    success: function (data) {
      $(".error-log").html('');
      window.open(url, '_blank');
    },
    error:function (xhr, ajaxOptions, thrownError) {
      $(".error-log").html(xhr.getResponseHeader("Status"));
    }
  });
}

The backend looks like this:

$db = $this->getRow($id);
if(!$db) {
  header('Status: No such row in database', true, 404);
  exit;
}

The problem I'm having is when my message contains special characters, such as ä, Ä, ö, Ö. Those characters are returned as %C3%A4. I tried setting the charset of the header but it didn't work:

header('Content-type: text/plain; charset=ISO-8859-1');
header('Content-type: text/plain; charset=UTF-8');

How can I return the correct characters? Everywhere I look people suggest changing the charset, but it doesn't seem to work.

Chris
  • 57,622
  • 19
  • 111
  • 137
  • Have you tried defining the datatype and contenttype attributes? – Neo Nov 14 '16 at 17:07
  • content-type should be `javascript/json` and charset should be `UTF-8`. – Jaquarh Nov 14 '16 at 17:12
  • @KDOT, didn't work. I'm just sending back plain text anyway, so it couldn't be `content-type`... – Chris Nov 14 '16 at 17:15
  • That's strange, everything I have tested seems to return the correct format. I don't think therefore it is your PHP code that is causing the convserion. [see it here](https://3v4l.org/cSYP3). Have you tired using [`unescape()`](http://stackoverflow.com/questions/25003217/using-encodeuri-vs-escape-for-utf-8-strings-in-javascript) in your response string? – Jaquarh Nov 14 '16 at 17:19

0 Answers0