简体   繁体   中英

Jquery Error parsing Json data

I'm having an error parsing a JSON response from PHP.

Javascript:

function getStock()
{
    var color = $("#select_color option:selected").val();
    var comp = "<?= $idComp ?>";

    $.post( "ajax/getStock.php", 
    { 
        idColor: color, 
        idComp: comp 
    }).done(function( data ) 
    {
        alert(data);
        var obj = jQuery.parseJSON(data);
        $('#stockReal').val(obj.StockReal);
        $('#stockMinimo').val(obj.StockMinimo);
        $('#stockMaximo').val(obj.StockMaximo);
    });
}

PHP:

<?php
    require_once "../../helper/db.php";
    require_once "../../helper/security.php";

    if($_SERVER['REQUEST_METHOD'] == 'POST') 
    {
        if(!empty($_POST['idColor']) && !empty($_POST['idComp']))
        {
            $idComponente = test_input($_POST['idComp']);
            $idColor = test_input($_POST['idColor']);


            $conn = db_connect($STOCKMANAGER);

            $sql = "SELECT * FROM TableStock
                    WHERE idComponente = '$idComponente'
                    AND idColor = '$idColor';";
            $stock = db_request_one($conn, $sql);

            if($stock !== 0)
            {
                $respuesta = array('StockReal'=>$stock->StockReal
                    ,'StockMinimo'=>$stock->StockMinimo
                    ,'StockMaximo'=>$stock->StockMaximo);
                echo json_encode($respuesta);
            }
            else
            {
                $respuesta = array('StockReal'=>''
                    ,'StockMinimo'=>''
                    ,'StockMaximo'=>'');
                echo json_encode($respuesta);
                //echo $respuesta;
            }
        }
    }
?>

The error:

Uncaught SyntaxError: Unexpected token ...    jquery-1.10.2.js:550

When I get data from alert for example I get:

{"StockReal":"33","StockMinimo":"0","StockMaximo":"55"}

This is a good Json data. But if I try to validate with http://jsonlint.com/ i get the next error message:

Parse error on line 1:

^
Expecting '{', '['

Also when I try to stringify the Json Data I get a lot of u0000\\ before and after the data.

like this:

"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000{\"StockReal\":\"33\",\"StockMinimo\":\"0\",\"StockMaximo\":\"55\"}\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"

Can anyone help me?

Have you done an output of what is returned from $respuesta array? print_r($respuesta) and see what's in your array.

Also make sure you have NO white space in your getStock.php file.

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