dop20345 2015-03-01 17:18
浏览 54

AJAX Get无效

I'm attempting to pull data from a mysql server with AJAX. This is the AJAX call:

function getAllTasks()
        {
            alert("Getting all tasks");

            $.ajax({
              type: "get",
              url: "ajax.php",
              dataType: "json",
              data: data,
              async: false,
              success: function(data){
                   alert('hi!');
                   //perform operation
                },
                error: function() {
                  alert('Error occurs!');
                }
            });
        }

and this is the PHP it's supposed to run:

header('Content-Type: application/json');



$conn = new mysqli('localhost', 'projectAdmin', 'admin', 'to_do_list')
    or die('Error connection to database');

    getAllTasks($conn);




function getAllTasks($conn)
{
$query = "SELECT * FROM tasks WHERE ID=1";

$result = mysqli_query($conn, $query);

while($row = $result->fetch_assoc())
{
    $tasks[] = $row;
}

echo json_encode($tasks);

}

It's not working. When I run the PHP alone, it works fine, and returns a JSON string in the browser (chrome). When I run the AJAX, nothing happens. I get the "getting all tasks" alert, and then it just doesn't work. The AJAX is called from the $(document).ready() function.

I don't know where I'm going wrong, I've looked at a bunch of posts on here and my AJAX code looks next to identical. Is it an issue with json_encode? The AJAX? Help, please.

  • 写回答

2条回答 默认 最新

  • doujia1904 2015-03-01 17:21
    关注

    Here:

    data: data,
    

    It looks that the data variable is undefined. Also I would recommend you removing the async: false option in order to make an asynchronous request instead of blocking the UI.

    Looking at the Console in your browser might also reveal some potential problems with your code.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么