简体   繁体   English

AJAX不返回任何数据

[英]AJAX isn't returning any data

still fairly new to AJAX. 仍然对AJAX来说还很陌生。 I feel I'm not grasping it completely. 我觉得我没有完全掌握它。 I want to submit partial data to the server, perform an SQL query and return the results. 我想向服务器提交部分数据,执行SQL查询并返回结果。 This is what I have so far: 这是我到目前为止的内容:

jQuery jQuery的

j$('select[name=agent_Name]').change(function(event) {
     event.preventDefault();
     var agentID = j$(this).val();
     post_data = {'agent_ID':agentID};
     console.log("About to post data to the server");
    j$.post('../include/booking_Modify.php', post_data, function(response){  
        if(response.type == 'AgDEpCd'){
            output = response.text;
            console.log(output);
        }
        if(response.type == 'error'){
            output = response.text;
            console.log(output);
        }
    }, 'json');     
});

PHP 的PHP

<?php
session_start();
require("../include/conn.php");
dbopen();
//check $_POST vars are set, exit if any missing
    if(!isset($_POST["agent_ID"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
        die($output);
    }

    $stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
    $stmt->bind_param('i', $_POST["agent_ID"]);   // bind variables to the parameter
    $stmt->execute();

    $row = $result->fetch_assoc();
    $AgDEpCd = $row['AgDEpCd'];
    $stmt->close();
    $output = json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
    die($output);
?>

I checked to make sure: the file path was correct. 我检查确认:文件路径正确。 var agentID = j$(this).val(); var agentID = j $(this).val(); actually grabs a value, which it does Manually entered the SQL query into PHPMyAdmin to ensure I was retrieving results. 实际获取一个值,它确实将值手动输入到PHPMyAdmin中以确保我正在检索结果。 I can't seem to return anything from the server. 我似乎无法从服务器返回任何东西。 I'm not sure this is even possible. 我不确定这是否有可能。 Please help! 请帮忙!

Normally I will do just echo and exit, short and faster. 通常,我只会做简​​短的回声和退出。 In beforehand entering response, just console.log and check out if it return any. 在预先输入响应中,只需输入console.log并检查它是否返回任何内容。 If it doesn't just check your php code, there is other error than the encoding output. 如果不只是检查您的php代码,还有其他错误,而不是编码输出。 Try it. 试试吧。

 <?php
    session_start();
    require("../include/conn.php");
    dbopen();
    //check $_POST vars are set, exit if any missing
        if(!isset($_POST["agent_ID"]))
        {
            echo json_encode(array('type'=>'error', 'text' => 'Nothing was selected!'));
            exit;
        }

        $stmt = $conn->prepare("SELECT AgDEpCd FROM AGENTS WHERE AgentID = ?");
        $stmt->bind_param('i', $_POST["agent_ID"]);   // bind variables to the parameter
        $stmt->execute();

        $row = $result->fetch_assoc();
        $AgDEpCd = $row['AgDEpCd'];
        $stmt->close();
        echo json_encode(array('type'=>'AgDEpCd', 'text' => $AgDEpCd));
        exit;
    ?>

You are not assigned result to $result variable. 您尚未将结果分配给$ result变量。

It should be, 它应该是,

$result = $stmt->execute();

Just echo the result set at the end of the php script. 只需在php脚本末尾回显结果集即可。 It will be assigned to the ajax response data. 它将被分配给ajax响应数据。

$data = array('type'=>'AgDEpCd', 'text' => $AgDEpCd);
echo json_encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM