简体   繁体   中英

`json_encode does not return a UTF8 character

I have a AJAX request sent to the PHP server, and PHP returns an array with json_encode back to the client.

The array is a simple 2-indexed array.

What I get from console.log() is this:

{"1":"\u00d9\u0081\u00db\u008c\u00d9\u0084\u00d8\u00af title \u00d9\u0086\u00d8\u00a8\u00d8\u00a7\u00db\u008c\u00d8\u00af \u00d8\u00ae\u00d8\u00a7\u00d9\u0084\u00db\u008c \u00d8\u00a8\u00d8\u00a7\u00d8\u00b4\u00d8\u00af.","0":"FAIL"}

I think this is ASCII charset ? I don't know but I know it is not persian character!!!!

Here is my PHP code:

header ('Content-type: text/html; charset=utf-8');
            $message = $validator->messages();
            $response[1] = utf8_encode($message->first());
            $response[0] = FAIL;
            echo json_encode(($response));

Though when I set the response to echo only $message->first() than a json object , it is ok and utf-8 fully

I think your problem is that your PHP is claiming a content type of text/html rather than application/json . If you change the type, it will tell the browser that it should parse the content as JSON and you will get an object in your AJAX handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM