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.