javascript - No response data after call a function -
i have file called login.php
, in file there box fill send login data server. when user press login button, code fired:
var posturl = globalvariables.baseurl + 'application/controllers/user.php/ajax_check_login'; var postdata = { 'username': $('#username').val(), 'password': $('#password').val() }; $.post(posturl, postdata, function(response) { // stuff.. }, 'json');
the posturl
valorized correctly by:
http://localhost/app_name/application/controllers/user.php/ajax_check_login
now have list of own controller allow me manage web app. 1 of controller user.php
, how can see call ajax_check_login
function. function check if data passed request exists in database , if correct. content:
public function ajax_check_login() { echo "test?"; try { if(!isset($_post['username']) || !isset($_post['password'])) { throw new exception('bad credentials!'); } $this->check_login($_post['username'], $_post['password']); if($user_data) { $output[] = array( "some content..." echo json_encode($output); } else { echo json_encode(ajax_failure); } } catch(exception $exc) { echo json_encode(array( 'exception ' => array(exceptiontojavascript($exc)) )); } }
now, @ top of inserted echo, test if printed. nothing. in chrome console get:
this request has no response data available.
i don't know wrong, enlighten me?
first, you'd check what's return server, http200 without contents? or error 500. why i'm saying because snippet tells there few variables/functions missing. check comments:
public function ajax_check_login() { echo "test?"; try { if(!isset($_post['username']) || !isset($_post['password'])) { throw new exception('bad credentials!'); } $this->check_login($_post['username'], $_post['password']); if($user_data) // $user_data never defined in function, , it's not global variable { $output[] = array( "some content..." // syntax error; here missing end parenthesis echo json_encode($output); } else { echo json_encode(ajax_failure); } } catch(exception $exc) { echo json_encode(array( 'exception ' => array(exceptiontojavascript($exc)) // unknown function exceptiontojavascript(); encounter fatal error )); } }
second, suggest send header define you're sending json encoded content, , exit() after echo. see example:
header('content-type: application/json'); echo json_encode(/** return **/); exit();
Comments
Post a Comment