我只是想知道是否有办法在用json_decode回显响应时绕过所有PHP消息。
我目前的问题是,如果我的PHP代码有任何回显或输出中包含的任何其他内容,除了数组之外,我的Javascript根本不起作用。
.PHP:
<?php
error_reporting(1);
$errors = array();
$data = [];
// data from angular to be handled and
// then if all goes well set submission to true to display with ng-show
$data["submission"] = true;
header('Content-Type:application/json;');
echo json_encode($data);
?>
.JS:
$scope.testProcessForm = function() {
$http({
method : 'POST',
url : 'reg.php',
data : $scope.formData,
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function(response) {
console.log(response);
$scope.submission = response.data.submission;
}, function(error) {
console.log('error', error);
我假设通过使用response.data.submit,我可以在那里访问该数据,但如上所述,如果包含任何不在数组中的PHP输出,代码就会中断。
是否可以只访问/响应$data数组,使其不会中断?
您可以在调用上次回显之前使用 ob_clean
(http://php.net/manual/en/function.ob-clean.php) 清理输出:
<?php
error_reporting(1);
// you also need to add ob_start()
ob_start();
$errors = array();
$data = [];
// data from angular to be handled and
// then if all goes well set submission to true to display with ng-show
$data["submission"] = true;
header('Content-Type:application/json;');
ob_clean();
echo json_encode($data);
?>
error_reporting();
应设置为0
=>error_reporting(0);
另请注意,所有可能导致错误的情况都应按语法处理,您可以发送错误代码(使用http_response_code(404/500)
)以及可以在客户端读取的响应。
error_reporting(0);
而不是
error_reporting(1);