4

I have a problem with json encoded information which loaded via ajax.

The PHP code (test.php):

<?php
  $val1 = 'Productmanager m / f';
  $val2 = 'test';
  $arr = array('first' => $val1, 'second' => $val2);
  echo json_encode($arr);
?>

The JavaScript code inside a html file:

$(document).ready(function() {
  $.post("test.php", function(data){
    var response = $.parseJSON(data);
    console.log(response.first);
    console.log(response.second);
  }
});

And the result in console looks like:

Productmanager&#x20;m&#x20;&#x2f;&#x20;f

and

test

Both files are UTF-8 encodet.

I´m realy dont know why and how to convert it back to a readable string. You may have an idea how this can occur?

I have found no suitable solution at first go, just search&replace approaches.

1
  • Works fine after update! Commented Apr 22, 2012 at 17:03

3 Answers 3

3

Add the correct PHP header and decode the string:

<?php
  header("Content-type: application/json");
  $val1 = "Productmanager m / f";
  $val2 = "test";
  $arr = array("first" => $val1, "second" => $val2);
  echo json_encode($arr);
?>

<script>

    $(document).ready(function() {
      $.post("test.php", function(data){
        var response = $.parseJSON(data);
        console.log(htmlDecode(response.first));
        console.log(response.second);
      }
    });

function htmlEncode(value){
  return $('<div/>').text(value).html();
}

function htmlDecode(value){
  return $('<div/>').html(value).text();
}

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

ok, then i have to remove the "var response = $.parseJSON(data);" and the result is the same
0

you can try this for your test.php

<?php
  $val1 = 'Productmanager m / f';
  $val2 = 'test';
  $arr = array('first' => $val1, 'second' => $val2);
  echo json_encode($arr, JSON_UNESCAPED_UNICODE);
?>

2 Comments

ends in an empty response :-/
you should have PHP version 5.4.0 to work with that JSON_UNESCAPED_UNICODE option so can you just try to run yout test.php file and see what output it is giving
0

Could you please try this?

$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "test.php",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        dataType: 'json',
        success: function(data) {
            var response = $.parseJSON(data);
                console.log(response.first);
                console.log(response.second);
        }
    });
});

You can set character encoding for ajax requests by using "contentType"

On your php side, your code must be like ;

<?php
  $val1 = 'Productmanager m / f';
  $val2 = 'test';
  $arr = array('first' => $val1, 'second' => $val2);
  echo json_encode($arr, JSON_UNESCAPED_UNICODE);
?>

Important note: JSON_UNESCAPED_UNICODE works with php 5.4.0!!

2 Comments

i try this, but have to remove the $.parseJSON, because the response is already parsed. the result was the same.
My webspace runs with PHP 5.3.10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.