Search code examples
javascriptphpjqueryajaxfat-free-framework

Fat-Free Framework + JQuery AJAX GET = no response


I have a mapped Fat-Free Framework class declared as follows in my index.php:

$f3->map('/user', 'User');

The User class looks like this:

<?php
class User {
  function __construct($f3) {
    $this->users = new \DB\SQL\Mapper($f3->db, 'users');
  }

  function get($f3) {
    return json_encode('Just some text');
  }

  function post($f3) {
    // There is tested, working code in here but I've omitted it for simplicity's sake
  }

  function put($f3) {
  }

  function delete() {
  }
}

I have my Javascript first loaded by app.js like this:

$(document).ready(function(){
  var currentPage = $(location).attr('pathname'),
      requiredJS = document.createElement('script'),
  requiredJS.type = 'text/javascript';

  switch(currentPage) {
    case '/mypage':
      requiredJS.src = 'myscript.js';
      $('body').append(requiredJS);
      break;
    // more cases...
  }
});

Then my simple AJAX call in mypage.js looks like this:

$.get('/user', function (data) {
  console.log(data);
});

When I go to the /mypage route, I get only an empty string in my console. Why is this happening? Where am I messing up?


Solution

  • You should echo the result:

    echo json_encode(array('Just some text'));