-1

I am trying to parse JSON from text using JSON.parse.

My problem is that my JSON is encoded to UTF8 but JSON.parse won't parse it with UTF8.

This is my build_workout_functions.js code:

function getExercies(){
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){

  if (xmlhttp.readyState==4 && xmlhttp.status==200){
    console.log(xmlhttp.responseText);
    var obj = JSON.parse(xmlhttp.responseText);
    console.log(obj);
      }
   }

xmlhttp.open("POST","get_exercise_list.php",true);

var e = document.getElementsByName("muscles");
var strUser = e[0].options[e[0].selectedIndex].text;

console.log(strUser);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send("muscle=" + strUser);
}

The PHP code:

<?php
header("Content-type: application/json; charset=utf-8");
if(isset($_POST['muscle'])){
    echo $_POST['muscle'];
    $response["test"] = $_POST['muscle'];
    echo json_encode($response);
}
?>

The HTML page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script src="build_workout_functions.js" charset="UTF-8"></script>
</head>
<body id="login-bg"> 
    <div class="workout_table">
        <select name="muscles" onchange="getExercies()">
            <option value="legs">טסט</option>
            <option value="back">טסט2</option>
        </select>
        <select name="exericse"><select>
    </div>
</body>
</html>

In my dev console (I'm using Chrome) I see this log:

יד קדימית build_workout_functions.js:33
XHR finished loading: "****************". 
build_workout_functions.js:38
יד קדימית{"test":"\u05d9\u05d3 \u05e7\u05d3\u05d9\u05de\u05d9\u05ea"} 

In my log I see that when I just echo my POST I can see that it encoded correctly.

But my JSON.parse didn't do it. How can I fix this?

hichris123
  • 10,145
  • 15
  • 56
  • 70
dasdasd
  • 1,971
  • 10
  • 45
  • 72
  • 2
    What exactly is the problem, what is parsed "incorrectly"? Btw, your log doesn't match your code. – Bergi Mar 01 '14 at 16:41
  • What is your php version ? – Nico Mar 01 '14 at 16:44
  • 1
    Do you get an error or something? It helps tremendously to *explain what you mean* when you say something "doesn't work". – Pointy Mar 01 '14 at 16:45
  • 1
    Maybe this link will help you : http://stackoverflow.com/questions/6771938/any-way-to-return-php-json-encode-with-encode-utf-8-and-not-unicode – Nico Mar 01 '14 at 16:46
  • I used this code: json_encode($response, JSON_UNESCAPED_UNICODE) and it fine. Thanks for helping – dasdasd Mar 01 '14 at 16:56
  • @Nico pointed to the proper place to find the answer. This should be closed as duplicate. – JAAulde Mar 02 '14 at 00:56

1 Answers1

0

I used this to solve the problem:

json_encode($response, JSON_UNESCAPED_UNICODE)
jogojapan
  • 68,383
  • 11
  • 101
  • 131
dasdasd
  • 1,971
  • 10
  • 45
  • 72