donglaoping9702 2013-07-02 16:41
浏览 49
已采纳

问题返回JSON格式PHP / AJAX

currently i'm building my own MVC to try and get a better understanding of the underlying principles of existing frameworks. However i've run into an issue with an AJAX get method i have and trying to output returned data into JSON. Here's the PHP and Javascript below:

public function ajaxGet(){
    $stat = $this->db->prepare("SELECT * FROM data");
    $stat->setFetchMode(PDO::FETCH_ASSOC); //fetch the data as an associative array
    $stat->execute();
    $data = $stat->fetchAll();
    echo json_encode($data);
    //since we are passing the data from a json encoded ajax request
    //we must format it that way here
}

And below is my javascript.

$(function(){

$.get("/mvc2/dashboard/ajaxGet", function(o){

    console.log(o); //for some reason i had to remove 'json' to get this to work??
    for(var i=0;i<1;i++){

        $('#listInserts').append('<div>'+o+'</div><br>');
    }

    }, 'json'); 
    //output in json format


$('#accountInsert').submit(function(){

    var url = $(this).attr('action');
    //this gets the value of action in our form
    //so we can pass all the data and hadle the request from there 
    var data = $(this).serialize(); 
    //The serialize() method creates a URL encoded text string by serializing form values.
    $.post(url, data, function(o){
        //post the url and the data
        //and have a call back function called 'o'
        console.log(url, data);
    });

    return false; 
    //return flase so the form data can be handled
    //through javascript
    //and so we don't refresh the page
});

});

Now please note, that when i remove the 'json' tag from my javascript function, the data is output an an associate array, and even with the tag there i can see that the data is returned in the function response from chrome. But no data is logged or alerted.

  • 写回答

2条回答 默认 最新

  • douzhendi4559 2013-07-02 16:56
    关注

    Try this:

    PHP:

    public function ajaxGet(){
        $stat = $this->db->prepare("SELECT * FROM data");
        $stat->execute();
        $data = $stat->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode($data);
        //since we are passing the data from a json encoded ajax request
        //we must format it that way here
    }
    

    JS:

    1. Be sure to comment out any console.log methods if you're not using the console
    2. In your callback for AJAX calls, check the console.log. Your o is the OBJECT of data returned.. with that said, you'll need to access the properties with something like o.colName from your PHP result.
    3. Ensure your MVC path is correct, I believe you said it is because you saw an array of sort.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行