【问题标题】:AJAX isn't returning any dataAJAX 不返回任何数据
【发布时间】:2014-11-13 06:04:11
【问题描述】:

对于 AJAX 来说还是相当新的。我觉得我没有完全掌握它。我想向服务器提交部分数据,执行 SQL 查询并返回结果。这是我目前所拥有的:

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);
?>

我检查以确保: 文件路径是正确的。 var agentID = j$(this).val();实际上抓住了一个价值,它确实做到了 手动将 SQL 查询输入 PHPMyAdmin 以确保我正在检索结果。 我似乎无法从服务器返回任何东西。我不确定这是否可能。请帮忙!

【问题讨论】:

  • 它缺少]。这是错字吗? 'select[name=agent_Name'
  • @Ghost 不错。但是不,我打了前几行。
  • 另外,只是在黑暗中拍摄,检查console.log(typeof response); 并检查结果。或者只是console.log(response)
  • 只是一个建议:如果您想测试输出,请将您的 console.logs 保留在如果块之外
  • 在 die 之前尝试 echo $output 并将 die($output) 更改为 die();

标签: php jquery mysql ajax json


【解决方案1】:

通常我只会回显并退出,短而快。在预先输入响应中,只需console.log 并检查它是否返回任何内容。如果它不只是检查您的 php 代码,那么除了编码输出之外还有其他错误。试试看。

 <?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;
    ?>

【讨论】:

    【解决方案2】:

    您没有将结果分配给 $result 变量。

    应该是,

    $result = $stmt->execute();
    

    【讨论】:

    • 我添加了以下代码,但仍然没有看到任何返回结果!
    【解决方案3】:

    只需在 php 脚本末尾回显结果集。 它将分配给 ajax 响应数据。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2014-06-03
      • 1970-01-01
      • 2017-11-23
      相关资源
      最近更新 更多