Invalid JSON Response from jQuery Server Side - Page 2

Invalid JSON Response from jQuery Server Side

2»

Answers

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Hello @allan, I am using in production server PHP version 7.0. In localhost is 7.0.10.

    @Rodidi yes, I am using some words like "irmãos". I did what you said:

    "mysql:host={$sql_details['host']};dbname={$sql_details['db']};charset=utf8mb4",

    and now I have this JSON, that is still invalid:

    {"draw":1,"recordsTotal":59,"recordsFiltered":59,"data":[["0","A & D Com\u00e9rcio de Materiais Did\u00e1ticos Ltda - ME","",null,null,"Pirassununga",null,"53"],["0","Adriana Scamillia Com\u00e9rcio de Cosm\u00e9ticos - ME","24.905.485\/0001-20","13643-320","Rua Irene Pegoraro Methner","Pirassununga","Jardim Terras de San Jos\u00e9","46"],["0","Andre dos Reis Andreotti Com\u00e9rcio de Sucatas - ME","14.669.660\/0001-74","13633-037","Avenida Cap. Antonio Joaquim Mendes","Pirassununga","Jd. Carlos Gomes","44"],["0","Andr\u00e9 Luis Rodrigues Alimentos - ME","23.543.447\/0001-02",null,null,"Pirassununga",null,"23"],["1","Andr\u00e9ia Aparecida Fioramonti","",null,null,"Pirassununga",null,"16"],["0","Arnaldo Luiz Pereira Curitiba - ME","04.828.304\/0001-00","13633-515","Rua Curitiba","Pirassununga","Vila Belmiro","43"],["0","Associa\u00e7\u00e3o de Capoeira de Pirassununga e Interior Paulista","21.165.305\/0001-23","13631-227","Rua Francisco Mollo","Pirassununga","Jardim Pavesi","45"],["1","Benvindo Flausino Alves","14.084.994\/0001-86",null,null,"Pirassununga",null,"27"],["1","Carlos Eduardo Paes - ME","02.222.209\/0001-60","13631-165","Rua Santos Dumont","Pirassununga","Jardim S\u00e3o Valentim","4"],["0","CELIO HALAN AMARO 34684045811","27.634.017\/0001-57","13630-614","RUA BENEDITO SAMPAIO","Pirassununga","JARDIM MARGARIDA","33"]]}array(4) { ["draw"]=> int(1) ["recordsTotal"]=> int(59) ["recordsFiltered"]=> int(59) ["data"]=> array(10) { [0]=> array(8) { [0]=> string(1) "0" [1]=> string(49) "A & D Comércio de Materiais Didáticos Ltda - ME" [2]=> string(0) "" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "53" } [1]=> array(8) { [0]=> string(1) "0" [1]=> string(47) "Adriana Scamillia Comércio de Cosméticos - ME" [2]=> string(18) "24.905.485/0001-20" [3]=> string(9) "13643-320" [4]=> string(26) "Rua Irene Pegoraro Methner" [5]=> string(12) "Pirassununga" [6]=> string(26) "Jardim Terras de San José" [7]=> string(2) "46" } [2]=> array(8) { [0]=> string(1) "0" [1]=> string(50) "Andre dos Reis Andreotti Comércio de Sucatas - ME" [2]=> string(18) "14.669.660/0001-74" [3]=> string(9) "13633-037" [4]=> string(35) "Avenida Cap. Antonio Joaquim Mendes" [5]=> string(12) "Pirassununga" [6]=> string(16) "Jd. Carlos Gomes" [7]=> string(2) "44" } [3]=> array(8) { [0]=> string(1) "0" [1]=> string(36) "André Luis Rodrigues Alimentos - ME" [2]=> string(18) "23.543.447/0001-02" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "23" } [4]=> array(8) { [0]=> string(1) "1" [1]=> string(29) "Andréia Aparecida Fioramonti" [2]=> string(0) "" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "16" } [5]=> array(8) { [0]=> string(1) "0" [1]=> string(34) "Arnaldo Luiz Pereira Curitiba - ME" [2]=> string(18) "04.828.304/0001-00" [3]=> string(9) "13633-515" [4]=> string(12) "Rua Curitiba" [5]=> string(12) "Pirassununga" [6]=> string(12) "Vila Belmiro" [7]=> string(2) "43" } [6]=> array(8) { [0]=> string(1) "0" [1]=> string(60) "Associação de Capoeira de Pirassununga e Interior Paulista" [2]=> string(18) "21.165.305/0001-23" [3]=> string(9) "13631-227" [4]=> string(19) "Rua Francisco Mollo" [5]=> string(12) "Pirassununga" [6]=> string(13) "Jardim Pavesi" [7]=> string(2) "45" } [7]=> array(8) { [0]=> string(1) "1" [1]=> string(23) "Benvindo Flausino Alves" [2]=> string(18) "14.084.994/0001-86" [3]=> NULL [4]=> NULL [5]=> string(12) "Pirassununga" [6]=> NULL [7]=> string(2) "27" } [8]=> array(8) { [0]=> string(1) "1" [1]=> string(24) "Carlos Eduardo Paes - ME" [2]=> string(18) "02.222.209/0001-60" [3]=> string(9) "13631-165" [4]=> string(17) "Rua Santos Dumont" [5]=> string(12) "Pirassununga" [6]=> string(20) "Jardim São Valentim" [7]=> string(1) "4" } [9]=> array(8) { [0]=> string(1) "0" [1]=> string(29) "CELIO HALAN AMARO 34684045811" [2]=> string(18) "27.634.017/0001-57" [3]=> string(9) "13630-614" [4]=> string(20) "RUA BENEDITO SAMPAIO" [5]=> string(12) "Pirassununga" [6]=> string(16) "JARDIM MARGARIDA" [7]=> string(2) "33" } } }
    
  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    Copy and paste it into this site:
    https://jsonlint.com/

    You will see what is invalid.

    Kevin

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    It's adding \/ and others caracters (\u00e9) when words have accentuation. How can I fix this @kthorngren?

    Error: Parse error on line 17:
    ...GARIDA", "33"] ]}array(4) { ["draw"]
    --------------------^
    Expecting 'EOF', '}', ',', ']', got 'undefined'

    And on line 17 there was pointing that there was an extra }. If I keep removing the suggestion, I end in removing everthing!

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    The JSON is appended with this starting on line 18:

    array(4) {
        ["draw"] => int(1)["recordsTotal"] => int(59)["recordsFiltered"] => int(59)["data"] => array(10) {
            [0] => array(8) {
                [0] => string(1)
                "0" [1] => string(49)
                "A & D Comércio de Materiais Didáticos Ltda - ME" [2] => string(0)
                "" [3] => NULL[4] => NULL[5] => string(12)
                "Pirassununga" [6] => NULL[7] => string(2)
                "53"
            }[1] => array(8) {
    .....
    

    If you remove lines 17 through 109 and "Validate" the JSON you will see its now valid.

    Kevin

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    I didn't understand what you said now. @kthorngren

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    Copy your JSON string into https://jsonlint.com/ again and Validate. You will get the above error. Jsonlint will reformat the string during validation. The valid portion of the string ends at line 17 with a }. That is the EOF mark but your string has additional characters starting on line 18. Remove everything after line 18 and revalidate. You will have a valid JSON string.

    Make sure your server side code does not append what is starting at line 18 to your JSON string.

    Kevin

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    Okay, thanks for your advice @kthorngren .

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Looks like you have some debug output that is marring the JSON return.

    Allan

  • zakkenwasserzakkenwasser Posts: 1Questions: 0Answers: 0

    I think it has something to do with the charset. Make sure you have this line in the <head> section:
    <meta charset="utf8">

    In your connection settings also put the charset=utf8:
    $connection = new PDO( 'mysql:host=localhost;dbname=mytest;charset=utf8', $username, $password );

    I could reproduce the error. When I omit the charset on both files I was looking at the message: "This request has no response data available" and the network response didn't display anything.

    Also strange, when I omit the charset, the first page just rendered well but clicking the second page (I had pagination on) it came up empty with the word "Processing . . ." and when I clicked the sixth page the data was spit out correctly.

    Perhaps somebody else can benefit from my posting and more important, can reproduce the behavior.

  • RaymhiRaymhi Posts: 1Questions: 0Answers: 0

    Hello,

    this problem is due to the class ssp.

    The default connection does not support utf8 and the accented characters are bugged next.

    Modify the file 'ssp.class.php' and replace in the function 'function sql_connect' the following code:

    $db = @new PDO(
                    "mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
                    $sql_details['user'],
                    $sql_details['pass'],
                    array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
                );
    

    By :

    $db = @new PDO(
                    "mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
                    $sql_details['user'],
                    $sql_details['pass'],
                    array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' )
                );
    
This discussion has been closed.