Answer the question
In order to leave comments, you need to log in
How to fix encoding of data received in PHP from JS via AJAX?
Hello. I am making an AJAX request from JSON data. If I output this data to the browser console, they are displayed correctly:
{"first_name":"Vanya","id":362770972,"last_name":"Makarchuk","name":"ivanmkrchk"}
But if I'm on the PHP side I try to decode this JSON, I get a JSON_ERROR_SYNTAX error, and when I save it to a file, I see
{"first_name":"p▓p╟pҐya▐","id":362770972,"last_name":"p°p╟p╨p ╟я─я┤я┐п╨","name":"ivanmkrchk"}
How can I fix this?
Answer the question
In order to leave comments, you need to log in
sandbox.onlinephpfunctions.com/code/17a24bc12ba7a6...
Try setting the encoding to utf-8
<?PHP
$data = /** **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
1) php.ini - default_charset = "UTF-8"
2) mb_internal_encoding("UTF-8");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question