AJAX 调用时来自 php 的 JSON 响应

问题描述 投票:0回答:4

我正在从java脚本进行ajax调用,并且尝试从php获取json响应,如果我将dataType设置为JSON,如果没有成功执行块,则执行ajax错误块,如果我尝试控制台,则不指定dataType。将响应记录在成功块中我什么也没得到

JS

let CurrentDate = Date();
console.log(CurrentDate);

jsonObject = {
   'TrackName' : 'Material Science',
    'TrackDesc' : 'Test Text Test Text Test Text Test Text Test Text Test Text Test Text ',
    'Timestamp' : CurrentDate
}
console.log(jsonObject);
$.ajax({
    type:'post',
    url:'../../../../PHP/adminScripts/addNewTrack.php',
    contentType: "application/json",
    data: {trackDetails:jsonObject},
    dataType: "json",
    success: function(response) {
        console.log('SUCCESS BLOCK');
        console.log(response);
    },
    error: function(response) {
        console.log('ERROR BLOCK');
        console.log(response);
    }
});

PHP

<?php
header('Content-type: application/json');
include('../connection.php');

if($_POST) {
$obj = $_POST['trackDetails'];

$TrackName = mysql_real_escape_string($obj['TrackName']);
$TrackDesc = mysql_real_escape_string($obj['TrackDesc']);
$TrackAdderID = 'Admin ';                  //$_SESSION["userID"];
$Timestamp = mysql_real_escape_string($obj['Timestamp']);

$response_array['status'] = 'status123';
echo json_encode($response_array);

请帮我弄清楚如何在 PHP 中获取 ajax 调用的 json 响应

javascript php jquery json ajax
4个回答
3
投票
  • 在你的 JS 文件中,删除
    contentType: "application/json",
  • 在您的 php 文件中,选中“包含文件 url”并
  • 正确关闭 if 语句块

JS 文件:

let CurrentDate = Date();
jsonObject = {
   'TrackName' : 'Material Science',
    'TrackDesc' : 'Test Text Test Text Test Text Test Text Test Text Test Text Test Text ',
    'Timestamp' : CurrentDate
}
$.ajax({
    type:'post',
    url:'addNewTrack.php',
    data: {trackDetails:jsonObject},
    dataType: "json",
    success: function(response) {
        console.log('SUCCESS BLOCK');
        console.log(response);
    },
    error: function(response) {
        console.log('ERROR BLOCK');
        console.log(response);
    }
});

PHP 文件:

<?php
    header('Content-type: application/json');
    include('../connection.php');

    if($_POST) {
        $obj = $_POST['trackDetails'];

        $TrackName = mysql_real_escape_string($obj['TrackName']);
        $TrackDesc = mysql_real_escape_string($obj['TrackDesc']);
        $TrackAdderID = 'Admin ';                  //$_SESSION["userID"];
        $Timestamp = mysql_real_escape_string($obj['Timestamp']);

        $response_array['status'] = 'status123';
        echo json_encode($response_array);
    }
?>

0
投票

从 JS 代码中删除

contentType: "application/json"
,并在 PHP 文件中正确关闭大括号,它将正常工作。


0
投票
<?php

$return_code = 1;// or 0
$message = 'Message';
$time = date("Y-m-d H:i:s");

echo '{"return_code":"'.$return_code.'","mesaj":"'.$message .'","t":"'.$time  .'"}';
?>

然后ajax中的javascript成功

<script>
success: function (e) {
const json = JSON.parse(e);
const validJSON = !!json;
}
</script>

json.t 表示时间,json.return_code


0
投票

您可以使用 json_encode() 函数将 PHP 数组转换为 JSON 格式并作为响应返回。发送 AJAX 请求时设置 dataType: 'JSON'

© www.soinside.com 2019 - 2024. All rights reserved.