So I'm still a newbie when it es 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
So I'm still a newbie when it es 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
Share Improve this question edited Nov 18, 2016 at 20:00 Mathieu Turcotte asked Nov 18, 2016 at 18:37 Mathieu TurcotteMathieu Turcotte 3542 silver badges14 bronze badges 8-
What is the data type of your
$package
object? – bassxzero Commented Nov 18, 2016 at 18:41 -
1
First, there is no such thing as an "ajax file". That's just a PHP file that you happen to be calling using ajax. Second, whatever your
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 withres
in the javascript code? Third, you are usingasync: false
in your ajax setup. This is generally not considered a good thing to do. – Patrick Q Commented Nov 18, 2016 at 18:44 - Instead of use return, have you tried "die(json_encode($package));" ? – Vítor Oliveira Commented Nov 18, 2016 at 18:44
-
In the
.success
function, why don't you log theresult
to see what the actual return object is. Whatever PHP is returning is not correctly formatted for JSON. – TheValyreanGroup Commented Nov 18, 2016 at 18:46 - bass: the type is tbProductPackage. Is a custom class. Patrick: I'm sorry I used the wrong term. However, my getInstanceByPackageSerial() has been touroughly tested, and returns exactly what it should return: an instance of the tbProductPackage class. I am well aware I'm doing something the wrong way here, hence the question! ;) – Mathieu Turcotte Commented Nov 18, 2016 at 18:46
3 Answers
Reset to default 7In getPackage.php
page :
echo json_encode($package);
not use return
In Jquery should be :
data: {packageSerial:ScannedCode},
After success not need $.parseJSON(
because getPackage.php
already retrieve json encode
so, is should be :
}).success(function(result) {
res = result
});
also add dataType: 'json',
after data: {packageSerial:ScannedCode},
So, Final correction code is :
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);
?>
Ajax expects the JSON inside the request body.
Use
die(json_encode($SOME_ARRAY_OR_OBJECT));
not "return" json_encode($SOME_ARRAY_OR_OBJECT)
I don't know how your database is done, but as a first step, you could make a SELECT query and fetch it as an array. Then, json_encode would work without problem. Something along the lines of:
$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 );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744084150a4555854.html
评论列表(0条)