I've set up an Ajax call in my component.
I seem to have got the initial call, the controller, the data model is working because when I make the JavaScript call I'm showing this in the code inspector as the response:
({"userblock":"none","fileName":"","fileLocat":"","username":null,"type":"current"}); But I can't seem to access the data via my JavaScript:
function initialise() { console.log(" start Ajax Call "); jQuery.ajax({ type: "POST", url: 'index.php?option=com_daily&task=ajax.initialisePerson&format=json', data: registration, success: function(response){ console.log("back from ajax");// this shows nothing console.log("response is "+response[0].userblock);// this shows nothing if(response.message !=""){.... I guess I'm doing something stupid but any help would be great.
The controller code is:
public function ajax() { $user = JFactory::getUser(); $jinput = JFactory::getApplication()->input; // Check Token! $token = JSession::getFormToken(); $call_token = $jinput->get('token', 0, 'ALNUM'); if($token == $call_token) { $task = $this->getTask(); switch($task) { case 'initialisePerson': try { $returnRaw = $jinput->get('raw', false, 'BOOLEAN'); $viewValue = $jinput->get('view', NULL, 'INT'); if($viewValue) { $result = $this->getModel('ajax')->displayDetails($viewValue); } else { $result = false; } if($callback = $jinput->get('callback', null, 'CMD')) { echo $callback . "(".json_encode($result).");"; } elseif($returnRaw) { echo json_encode($result); } else { echo "(".json_encode($result).");"; } } catch(Exception $e) { if($callback = $jinput->get('callback', null, 'CMD')) { echo $callback."(".json_encode($e).");"; } else { echo "(".json_encode($e).");"; } } break; } } So doing this led me to the problem:
echo "(".json_encode($e).");"; I used this code and it should be
echo json_encode($result); All works now.