weixin_33671935 2018-05-12 15:32 采纳率: 0%
浏览 53

使用SQL数据进行Ajax调用

I call ajax at my action.php file to get one data from DB and load it dynamically.

$.ajax({
        url: 'ajax.php', 
        type: "POST",
        data: "localid="+ <?php echo $prelocal; ?>,
        dataType: 'json',
        success: function(response){
            console.log("repo: "+response);
        }
});  

My ajax.php is quite simple. I added if condition to check if I will call that file directly, and it is working

<?php 
$localid = $_POST['localid'];
if(empty($localid)) {
  $localid = 1;
}
$i = "SELECT `userid` FROM `table` WHERE `localid` = '{$localid}'";
$ri = $conn->query($i);
$v = $ri->fetch_assoc();
echo json_encode($v);
?>

Call ajax.php directly giving me response

{"userid":"4"}  

But at action.php I do not get any console.log response (why?). What I want to do, is to get only digit as value to input. May I ask for hint?

  • 写回答

1条回答 默认 最新

  • 胖鸭 2018-05-12 16:22
    关注

    You need to change your javascript as following.

    var local_id = <?php echo $prelocal; ?>;
    $.ajax({
            url: 'ajax.php',
            dataType: 'json',
            type: 'POST',
            data: {localid:local_id},
            success: function(data) {
                console.log(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥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时被拒绝