So I'm still a newbie when it comes to javascript and php. I am having this issue:
from javascript, I scan a package's barcode using a barcode reader. I send this to an PHP file using ajax, Which builds an object, and needs to return it to my javascript code.
I'm doing this:
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;
}
The 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% certain my object gets built properly. I did do a var_dump of my $package object, and everything is fine with it. However, when trying to get it back to javascript, I tried a bunch of different things, nothing works.
The $.parseJSON(result); statement seems to be giving me this error:
Uncaught SyntaxError: Unexpected end of JSON input
I also tried to use serialize(), but I get an error message:
Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances'
Basically, My database is in my object, I'm guessing I can't serialize it...
What am I doing wrong here?
Thank you
$packageobject?getInstanceByPackageSerial()function is returning either isn't what you think it is, or the way you're trying to do this isn't the right way. It would be helpful to know what your end objective is here. What do you end up trying to do withresin the javascript code? Third, you are usingasync: falsein your ajax setup. This is generally not considered a good thing to do..successfunction, why don't you log theresultto see what the actual return object is. Whatever PHP is returning is not correctly formatted for JSON.