为什么我的JSON没有正确地从PHP返回?

时间:2021-11-28 07:37:28

I am making an Ajax call from JS file to a PHP function in another file. Though the Ajax call is successful, in that it saves the record, I am not getting back the correct data for processing. Instead I get the html from the page. I need to be able to check the status so that I can then move forward if successful or notify the user if there was any kind of error that didn't save the record. Here is my JS code.

我正在从JS文件到另一个文件中的PHP函数进行Ajax调用。虽然Ajax调用是成功的,因为它保存了记录,但我没有收回正确的数据进行处理。相反,我从页面获取html。我需要能够检查状态,以便我可以在成功时继续前进,或者通知用户是否存在任何未保存记录的错误。这是我的JS代码。

 $.ajax({
            type: "POST",
            url: "utils.php?do=saveTick",
            data: {'dtData': JSON.stringify(sendData)},
            success: function (data) {
                alert(data.status);
                if (data.status != "SUCCESS") {
                    $("#updateDiv").dialog('destroy');
                    alert("Failed!");
                    $("#btnPrint").attr('disabled', false);
                    return false;
                } else {
                    me.confirmSave();
                }
            }
        }, 'json'
                );

And after processing through PHP, this is where I am trying to return my JSON so I can check it. In I try/catch I have this

在通过PHP处理之后,我正在尝试返回我的JSON,以便我可以检查它。在我尝试/捕捉我有这个

try {
        $data = json_decode($_REQUEST['dtData']);
        $values = $data[0];
        $ordNbr = $values->ordnbr;
        $saveTick = Do something and save the ticket
        // if ($saveTick->status === "SUCCESS") {
        $this->getSalesData($values, $ordNbr);
        echo json_encode($saveTick);

    } catch (Exception $e) {
        $saveTick = array('status' => 'FAILED');
        echo json_encode($saveTick);
    }

And this is what is returned to the console from my console.log above

这是从我上面的console.log返回到控制台的内容

 <html>
<head>
<title></title>

<style>

    TR.main_header {
        background-color:black; 
        color:white;
    }
    TR.main_footer {
        background-color:#396B42; 
        color:white;
    }
    div.error {
        color: red;
        font-weight: bold;
    }

</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr class="main_header">
        <td align="left" width="25%"></td>
        <td align="right" width="25%">Tuesday May 31,2016</td>

    </tr>
</table>
                                                                           [{"shipdate":"2016-05-31","ordnbr":"BR549","custid":"CUST","deltcktno":"D333423","siteid":"Some Site","dttype":"REG","IsExport":true,"slsperid":"","slsname":"Some Sales","slsemail":"me@me.com"}]{"status":"SUCCESS","message":"Successfully called","data":null,"count":0}
</body>
</html>

1 个解决方案

#1


2  

Get rid of the auto_prepend_file and auto_append_file settings in php.ini. These would be safe if they just defined variables and functions, but if they output HTML they'll interfere with scripts that are supposed to return other types of output, such as JSON or XML.

摆脱php.ini中的auto_prepend_file和auto_append_file设置。如果它们只是定义了变量和函数,那么这些将是安全的,但如果它们输出HTML,它们将干扰应该返回其他类型输出的脚本,例如JSON或XML。

You'll have to recode all your other scripts to require their headers and footers explicitly instead of relying on this PHP setting.

您必须重新编码所有其他脚本以明确要求其页眉和页脚,而不是依赖此PHP设置。

#1


2  

Get rid of the auto_prepend_file and auto_append_file settings in php.ini. These would be safe if they just defined variables and functions, but if they output HTML they'll interfere with scripts that are supposed to return other types of output, such as JSON or XML.

摆脱php.ini中的auto_prepend_file和auto_append_file设置。如果它们只是定义了变量和函数,那么这些将是安全的,但如果它们输出HTML,它们将干扰应该返回其他类型输出的脚本,例如JSON或XML。

You'll have to recode all your other scripts to require their headers and footers explicitly instead of relying on this PHP setting.

您必须重新编码所有其他脚本以明确要求其页眉和页脚,而不是依赖此PHP设置。