1

I am having following Jquery coding,

$.ajax({
          url: "dataAdd.php",
          type: "POST",
          data: data,
          contentType: "application/x-www-form-urlencoded; charset=UTF-8",
          cache: false,
          dataType: "json",

          success: function(data) {
          $("#result").html(data["result"]);
          },

          error : function(jqXHR, textStatus, errorThrown) {
                alert('Error: ' + jqXHR.status);
            }, 

 });

and having the following PHP coding,

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT');
header('Content-type: application/json; charset=UTF-8');
session_start();

if(isset($_SESSION['username']))
{
$sess_tab = trim($_SESSION['username']);
}
$data = // Some result stuff //;
$data = array("result"=>$ress);
echo json_encode($data);

This coding perfectly sends back all data in English(text or URL). But when it comes to handle Chinese, Japanese or other foreign language wikipedia URLs, the result is:

THIS RESULT IS OBSERVED ON FIREBUG, {result: null}

But for English text with little foreign text, we get the following response properly,

{"result":"<table><tr><td align=\"center\"><div id=\"btn_mypage\"><a href=\"http:\/\/www.fran.com\" target=\"_blank\">Fran\u00e7aise Fran\u00e7ais<\/a><\/div><\/td><td align=\"center\"><div id=\"btn_mypage\"><a href=\"http:\/\/ru.wikipedia.org\/wiki\/%D0%97%D0%B0%D0%B3%D0%BB%D0%B0%D0%B2%D0%‌​BD%D0%B0%D1%8F_%D1%81\" target=\"_blank\">Portada<\/a><\/div><\/td><td align=\"center\"><\/td><td align=\"center\"><\/td><\/tr><\/table>"}

Note: Our contentType is contentType: "application/x-www-form-urlencoded; charset=UTF-8", as Jquery doc specifies.

We have set dataType, header and UTF-8 in our code. The ajax json Request is always successful with all data but response sucks. Anybody faced similar situation and any suggestions?

If you find any mistakes in the query, freely edit it.

Does anybody have any idea about how we can modify the below coding to accept or receive all languages in ajax json response? This is found in another stackoverflow question.

<?php
header("Content-type: text/csv; charset=GB2312");
$arr = array('丂','亐');
echo json_encode($arr);
?>

This question seems to be in need of expert jquery ajax programmers. Shall we move the question status to Jquery Ajax team?

webblover
  • 1,196
  • 2
  • 12
  • 30
  • Are you sure `$ress` *isn't* null on the PHP side? – icktoofay Sep 07 '13 at 04:08
  • Yes, I have tested it on English, it returns back perfectly. But when it comes to send back non-English text or URLs particularly long ones, it returns null otherwise it returns full resultset without any problem. I find it strange that Ajax json could not handly foreign text(long ones not short more than 10 characters) but works for Chinese text with 5 or 6 characters long. – webblover Sep 07 '13 at 04:10
  • `real Challenge for you` where is `$ress` defined and assigned? – Yogesh Suthar Sep 07 '13 at 04:10
  • So then the problem is probably in the “some result stuff” section, no? – icktoofay Sep 07 '13 at 04:10
  • No not in "some result stuff" neither in $ress because we have set header type to header('Content-type: application/json; charset=UTF-8'); is an indication it should accept all types of resultset, right? – webblover Sep 07 '13 at 04:13
  • 1
    After the json_encode statement add checking for any json errors http://www.php.net/manual/en/function.json-last-error.php and http://www.php.net/manual/en/function.json-last-error-msg.php –  Sep 07 '13 at 04:14
  • variable `data` in `data: data,` is defined or not? – undone Sep 07 '13 at 04:17
  • no "data" is defined, dude. So only we get resultset for english(text) but the problem is it doesn't send back non-english text. – webblover Sep 07 '13 at 04:24
  • could you post part of your no english json, `echo json_encode($data);` – Emilio Gort Sep 07 '13 at 04:26
  • 2
    `Challenge?, did you google it` http://stackoverflow.com/questions/16398041/php-json-encode-not-working-with-chinese-character-set – Emilio Gort Sep 07 '13 at 04:28
  • hi Emilio, I post the response for English text but it may contain little foreign text. Here it is: {"result":"
    <\/td><\/td><\/tr><\/table>"}
    – webblover Sep 07 '13 at 04:32
  • for now forget about jquery, and in network resuls are you getting proper charachters – dev.meghraj Sep 07 '13 at 04:33
  • Meghraj, yes we are able to get the above response for short Russian text. But when the text length is more than 8 or so, we get {"result": null} Response. We are really interested to find SOLUTION for this type of issues. – webblover Sep 07 '13 at 04:36

1 Answers1

2

Wow,

I found the solution.

Just use PHP function mb_convert_encoding before sending back ajax-json response. I thank all for taking time to analyze this problem.

PHP coding at sending back JSON response:

<?php 
// Add this line as we are sending back html with non-english response before the last two lines.
$ress =$info = mb_convert_encoding($ress, "HTML-ENTITIES", "UTF-8");
            
$data = array("result"=>$ress);
echo json_encode($data);
?>
Community
  • 1
  • 1
webblover
  • 1,196
  • 2
  • 12
  • 30
  • 1
    I'm glad you found the answer in the link that I post in the comments, you could give credit to the author @YogeshSuthar [Answer](http://stackoverflow.com/questions/16398041/php-json-encode-not-working-with-chinese-character-set?answertab=oldest#tab-top) – Emilio Gort Sep 07 '13 at 18:50