mb_convert_encoding not working sometimes: Uncaught URIError: URI malformed

274 Views Asked by At

mb_convert_encoding() is not working for Cyrillic characters and other special characters if used with PHP and jQuery AJAX. The error is as follows:

Uncaught URIError: URI malformed
at decodeURIComponent ()
at Object. (popular:3725)
at u (jquery-3.3.1.min.js:12)
at Object.fireWith [as resolveWith] (jquery-3.3.1.min.js:12)
at k (jquery-3.3.1.min.js:12)
at XMLHttpRequest. (jquery-3.3.1.min.js:12)

My question is whether I need to decode the response value in jQuery or not.

$str = 'Hello Frèdèrï, How are you.';
$response = mb_convert_encoding($str, 'UTF-8', 'auto');
$this->json_render($response);
$.ajax({
  url: 'page.php?page=' + page,
  type: "get",
  timeout: 30000,
  tryCount: 0,
  retryLimit: 3,
  beforeSend: function () {
    $('.ajax-load').show();
    $('.load-more').hide();
  },
}).done(function (data) {
  if ($.trim(data) == "") {
    last_page = true;
    $('.ajax-load').hide();
    $('.no-data-found').show();
    return;
  }
  $('.ajax-load').hide();
  $('.load-more').show();
  var content = $(decodeURIComponent(data));
  $grid.append(content).masonry('appended', content);
  $("img.lazyload").myLazyLoad();
  $('.modal').modal();
  loadAddThis();
})
0

There are 0 best solutions below