执行ajax发布数据并在控制台日志

时间:2017-08-24 13:44:51

标签: javascript php jquery json ajax

我被要求对php脚本执行ajax post数据,该脚本将包含执行sql连接到数据库并获取所有数据并将数据转换为json格式的另一个脚本。然后json数据将显示在控制台上。另外我还被要求修改ajax以分别发布名称和宗教信仰的值,例如abdullah和muslim。我想在passwrapper上执行编码以获取并在console.log上显示数据。 在ajax.html

   <html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        dataType: "json",
        data: {
            lastName: 'Abdullah',
            lastReligion: 'Muslim',
        },      
        success: function(data){
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
        },

    });
};
</script>
</body>
</html>

在passwrapper.php中

       <?php
include 'student.php';
executePass();

receivePost();
function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    } 
    else 
    {
        echo '<script>console.log("Last='.$_POST["lastName"].' lastReligion='.$_POST["lastReligion"].'");</script>';

    }
}

?>

在student.php中

<?php
function executePass()
{

    $conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
    $db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

    $result = mysqli_query($conn,"select * from student");
    $json_array = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $json_array[] = $row;
    }

    echo json_encode($json_array);
}
?>

我的问题是如何在控制台日志上显示所有数据,并在console.log上显示发布数据..请不要修改student.php ...只修改passwrapper.php

1 个答案:

答案 0 :(得分:0)

这应该输出两个函数组合的数据,作为你的ajax“success”函数可以记录的JSON。您现有的代码存在问题,因为它的一部分尝试返回JSON,另一部分尝试返回<script>块,这是无效的JSON,并且为了安全起见也可能不会被浏览器执行的原因。

我还修改了这两个函数,因此它们将输出作为PHP变量返回给调用者,而不是直接将JSON字符串回显到浏览器。这使得它们更易于重复使用,并且使得将结果组合成单个连贯的JSON对象以输出到浏览器变得更加简单。

现有ajax“success”函数中的console.log(data);命令将负责将所有返回的数据记录到浏览器控制台。

$studentArr = executePass();
$postArr = receivePost();

echo json_encode(array("students" => $studentArr, "postvars" => $postArr));

function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    } 
    else 
    {
        return array ("lastName" => $_POST["lastName"], "lastReligion" => $_POST["lastReligion"]);
    }
}

function executePass()
{

    $conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
    $db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

    $result = mysqli_query($conn,"select * from student");
    $json_array = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $json_array[] = $row;
    }


    return $json_array;
}

现在,我不知道你的“学生”数据的确切结构,所以我不能给你一个你将收到的输出的确切例子,但如果我假设你的学生表有3个简单的字段 - “id”,“firstname”和“lastname”,表中有4行,你会得到这样的最终JSON输出:

{
    "students":
    [
        {
            "id": 1,
            "firstname": "firstname1",
            "lastname": "lastname1"
        },
        {
            "id": 2,
            "firstname": "firstname2",
            "lastname": "lastname2"
        },
        {
            "id": 3,
            "firstname": "firstname3",
            "lastname": "lastname3"
        },
        {
            "id": 4,
            "firstname": "firstname4",
            "lastname": "lastname4"
        }
    ],
    "postvars": {
        "lastName": "Abdullah",
        "lastReligion": "Muslim"
    }
}

您有一个具有两个属性的JSON对象。 “学生”属性包含表格中所有学生的数组。 “postvars”属性是另一个包含与您要捕获的两个POST变量匹配的属性的对象。