G
G
greenTransistor2019-01-10 17:49:05
PHP
greenTransistor, 2019-01-10 17:49:05

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

4 answer(s)
A
Antony Tkachenko, 2019-01-10
@greenTransistor

sandbox.onlinephpfunctions.com/code/17a24bc12ba7a6...

A
Andrey Fedorov, 2019-01-10
@aliencash

iconv

D
Dies Irae, 2019-01-10
@YumeReaver

Try setting the encoding to utf-8
<?PHP
$data = /** **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);

T
ThunderCat, 2019-01-10
@ThunderCat

1) php.ini - default_charset = "UTF-8"
2) mb_internal_encoding("UTF-8");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question