weixin_33671935 2014-11-13 06:04 采纳率: 0%
浏览 41

AJAX不返回任何数据

still fairly new to 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. This is what I have so far:

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
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(); actually grabs a value, which it does Manually entered the SQL query into PHPMyAdmin to ensure I was retrieving results. I can't seem to return anything from the server. I'm not sure this is even possible. Please help!

  • 写回答

3条回答 默认 最新

  • weixin_33696822 2014-11-13 06:15
    关注

    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. If it doesn't just check your php code, there is other error than the encoding output. 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;
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝