ajax Json request failed

For programming and general questions on Zend Framework

ajax Json request failed

Postby anto8383 on Thu Nov 16, 2017 4:15 am

Hi,
Please could you help me????
I try to use ajax json to display data from mysql, but the request is allways failed.
Please, someone could help me ?
here is my code: using Zend 3
//In my Controller
public function remplirAction(){
$data = $this->entityManager->getRepository(Entite::class)->findAll();
$request = $this->getRequest();
$query = $request->getQuery();
if ($request->isXmlHttpRequest()) {
$jsonData = array();
$idx = 0;
foreach($data as $sampledata) {
$temp = array(
'id' => $sampledata->getId(),
'nom' => $sampledata->getNom(),
'adresse' => $sampledata->getAdresse()
);
$jsonData[$idx++] = $temp;
}
$view = new JsonModel($jsonData);
$view->setTerminal(true);
} else {
$view = new ViewModel();
}
return $view;
}

//in my view : index.phtml
<script language = "javascript">
$(document).ready(function(){
$("#loadbook").on("click", function(event){
$.ajax({
url: '/remplir',
type: 'POST',
dataType: 'json',
async: true,

success: function(data, status) {
var e = $('<tr><th>ID</th><th>Nom</th><th>Adresse</th></tr>');
$('#book').html('');
$('#book').append(e);

for(i = 0; i < data.length; i++) {
book = data[i];
var e = $('<tr><td id = "id"></td><td id = "nom"></td><td id = "adresse"></td></tr>');
$('#id', e).html(book['id']);
$('#nom', e).html(book['nom']);
$('#adresse', e).html(book['adresse']);
$('#book').append(e);
}
},
error : function(xhr, textStatus, errorThrown) {
alert('Ajax request failed.');
}
});
});
});
</script>

Thanks in advance Guys.

Gabriel
anto8383
 
Posts: 1
Joined: Thu Nov 16, 2017 4:05 am

Return to Zend Framework

Who is online

Users browsing this forum: Google [Bot] and 4 guests