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 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致