mardi 28 avril 2020

php json_encode() works well. However, the results are broken on the client side

I am working on saving the query results from mysql in an array from php and sending the array to the client in json form. All the character sets, including mysql character set, are utf8, and after checking the results after json_encode(), an invalid json string came out.

$final_result = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE);
echo $final_result; // == valid json data

After checking using the json validator, I found that a valid json data was created.

However, after the echo call, I received the data from the client to ajax and checked it, and the following error occurred, so I cannot parse it.

SyntaxError: Unexpected token  in JSON at position 64362
    at parse (<anonymous>)
    at jquery.min.js:2
    at l (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

enter image description here

lastime":"2020-03-19 08:55:10�����"

As a result of checking, the characters shown in the picture were created in the middle, so the parsing is not working.

$.ajax({
        url: 'test.php',
        data: {action: 'get_Data'},
        type: 'post',
        dataType: 'json'
    }).done(function (json_text) {
        alert("done");
        console.log(json_text);
        json_Obj = JSON.parse(json_text);
        console.log(json_Obj);
    }).fail(function (xhr, status, errorThrown) {
        alert("fail");
        console.log(xhr);
        console.log(xhr.responseText);
        console.log(status);
        console.log(errorThrown);
    });

Of course, I declared the data type json at the place where I called Ajax Post.

I don't know why well-encoded data in php-side is called jquery, ajax on the client side, and why the following happens.

Can you tell me what I missed?




Aucun commentaire:

Enregistrer un commentaire