Returning a PHP object from ajax file to javascript code

So I'm still a newbie when it comes to javascript and php. I have a problem:

from javascript, I am viewing the package barcode using a barcode reader. I am sending this to a PHP file using ajax, which builds an object and should return it to javascript code.

I'm doing it:

function LoadPackage(ScannedCode) {
    var res;

    console.time("Load package " + ScannedCode);
    $.ajax({
        type: "POST",
        url: "ajax/3gmodule_inventory_ajax/getPackage.php",
        data: "packageSerial=" + ScannedCode,
        cache: false,
        async: false //inline operation, cannot keep processing during the execution of the AJAX
    }).success(function(result) {
        res = $.parseJSON(result);
    });

    console.timeEnd("Load package " + ScannedCode);

    return res;
}

Php file:

    <?php
        include_once "../../init.php";

        $packageSerial = $_POST["packageSerial"];

        $package = tbProductPackage::getInstanceByPackageSerial($packageSerial, $db);
        return json_encode($package);
// edit: first part of the problem was here, I was supposed to ECHO here. not RETURN.
    ?> 

I am 100% sure that my object is correctly built. I did var_dump of my $ package object and everything is fine with it. However, trying to return it to javascript, I tried a bunch of different things, nothing works.

$ .parseJSON (result); seems to be giving me this error:

Uncaught SyntaxError: Unexpected end of JSON input

I also tried using serialize (), but I get an error:

Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances'

Basically, my database is in my object, I assume that I can not serialize it ...

?

+4
3

getPackage.php :

echo json_encode($package);

return

JQuery :

data: {packageSerial:ScannedCode},

$.parseJSON(, getPackage.php json encode

, :

}).success(function(result) {
    res = result
});

dataType: 'json', data: {packageSerial:ScannedCode},

, :

JQuery:

function LoadPackage(ScannedCode) {
    var res;

    console.time("Load package " + ScannedCode);

    $.ajax({
        context: this,
        type: "POST",
        url: "ajax/3gmodule_inventory_ajax/getPackage.php",
        data: {packageSerial:ScannedCode},
        dataType: 'json',
    }).success(function(result) {
        res = result;
    });

    console.timeEnd("Load package " + ScannedCode);

    return res;
}

PHP:

<?php
    include_once "../../init.php";

    $packageSerial = $_POST["packageSerial"];

    $package = tbProductPackage::getInstanceByPackageSerial($packageSerial, $db);
    echo json_encode($package);
?>
+5

Ajax , JSON .

die(json_encode($SOME_ARRAY_OR_OBJECT));

not "return" json_encode($SOME_ARRAY_OR_OBJECT)

0

, , SELECT . json_encode . - :

$vec = array();
$query="SELECT * From YourTable
                    WHERE 1";

$result= mysql_query($query) or die(mysql_error() . $query)

while ($r=mysql_fetch_array($result)) {
   $vec [] = $r;
}

$toreturn=json_encode($vec );
-1

Source: https://habr.com/ru/post/1661228/


All Articles