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);
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据