将一个PHP对象从ajax文件返回到我的javascript代码

时间:2023-01-03 20:19:50

So I'm still a newbie when it comes to javascript and php. I am having this issue:

所以当涉及到javascript和php时,我仍然是一个新手。我有这个问题:

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.

从javascript,我使用条形码阅读器扫描包的条形码。我使用ajax将其发送到PHP文件,它构建一个对象,并需要将其返回到我的javascript代码。

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文件:

    <?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.

我100%确定我的对象是否正确构建。我确实做了我的$ package对象的var_dump,一切都很好。然而,当试图将它恢复到javascript时,我尝试了一堆不同的东西,没有任何作用。

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

$ .parseJSON(结果);声明似乎给了我这个错误:

Uncaught SyntaxError: Unexpected end of JSON input

I also tried to use serialize(), but I get an error message:

我也尝试使用serialize(),但是我收到一条错误消息:

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

谢谢

3 个解决方案

#1


6  

In getPackage.php page :

在getPackage.php页面中:

echo json_encode($package);

not use return

不要使用退货

In Jquery should be :

在Jquery应该是:

data: {packageSerial:ScannedCode},

After success not need $.parseJSON( because getPackage.php already retrieve json encode

成功后不需要$ .parseJSON(因为getPackage.php已经检索了json编码

so, is should be :

所以,应该是:

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

also add dataType: 'json', after data: {packageSerial:ScannedCode},

还要在data:{packageSerial:ScannedCode}之后添加dataType:'json',

So, Final correction code is :

所以,最终修正代码是:

Jquery :

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:

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

    $packageSerial = $_POST["packageSerial"];

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

#2


0  

Ajax expects the JSON inside the request body.

Ajax期望请求体内有JSON。

Use

使用

die(json_encode($SOME_ARRAY_OR_OBJECT));

not "return" json_encode($SOME_ARRAY_OR_OBJECT)

不是“返回”json_encode($ SOME_ARRAY_OR_OBJECT)

#3


-1  

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:

我不知道你的数据库是如何完成的,但作为第一步,你可以进行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


6  

In getPackage.php page :

在getPackage.php页面中:

echo json_encode($package);

not use return

不要使用退货

In Jquery should be :

在Jquery应该是:

data: {packageSerial:ScannedCode},

After success not need $.parseJSON( because getPackage.php already retrieve json encode

成功后不需要$ .parseJSON(因为getPackage.php已经检索了json编码

so, is should be :

所以,应该是:

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

also add dataType: 'json', after data: {packageSerial:ScannedCode},

还要在data:{packageSerial:ScannedCode}之后添加dataType:'json',

So, Final correction code is :

所以,最终修正代码是:

Jquery :

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:

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

    $packageSerial = $_POST["packageSerial"];

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

#2


0  

Ajax expects the JSON inside the request body.

Ajax期望请求体内有JSON。

Use

使用

die(json_encode($SOME_ARRAY_OR_OBJECT));

not "return" json_encode($SOME_ARRAY_OR_OBJECT)

不是“返回”json_encode($ SOME_ARRAY_OR_OBJECT)

#3


-1  

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:

我不知道你的数据库是如何完成的,但作为第一步,你可以进行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 );