0

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();
})
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ethane
  • 3
  • 3
  • What's the exact value of `data` as returned from `$response`? Also note that `decodeURIComponent` has nothing to do with jQuery, so I've added the 'javascript' tag too. – Rory McCrossan Dec 19 '18 at 09:46
  • $response contains the encoded string and it is not an issue. my question is do i need to decode this response in my Ajax code. i am using this - var content = $(decodeURIComponent(data)); – Ethane Dec 19 '18 at 09:49
  • It depends on the format of the value, which is why I asked to see it. If it contains special characters and you've encoded it, then yes you'll need to decode it (example: http://jsfiddle.net/Lubosxd9/). Otherwise you don't. – Rory McCrossan Dec 19 '18 at 09:51
  • @RoryMcCrossan, thanks for your quick response. function mb_convert_encoding() is used in a loop, so there are few strings which contain special characters not all strings. so how do i detect that which string need to encode – Ethane Dec 19 '18 at 10:05
  • Ok in that case you would be better off encoding them all and decoding on the client side - as you currently do. The issue then becomes why `decodeURIComponent()` does not like the format you're providing values in. Again, we'd need to see an example of `data` which throws this error in order to debug it. – Rory McCrossan Dec 19 '18 at 10:08
  • in past i have used `utf8_encode` function in place of `mb_converting_encoding` but it requires `decodeURIComponent`. Otherwise incase of special characters without using decode unicode character was displayed by browser. But it was not working in case of Cyrillic Character. So i just moved to `mb_converting_encoding`. But the fact is its not working in all cases when i am using decodeURIComponent with that. – Ethane Dec 19 '18 at 12:34

0 Answers0