example:
js:
function addListeArticleRequest() {
var paramRaw = {};
paramRaw['tarifClient'] = parseInt($("#tarif-c").text());
paramRaw['selArticleCode'] = selectedArticleCode;
var paramJson = JSON.stringify(paramRaw);
$.ajax({
type: "POST",
dataType: 'json',
data: {
param: paramJson
},
headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'},
url: 'localhost/appli/devis/newListArticle/',
complete: function (response) {
var rep = JSON.parse(response.responseText);
var testResponse = rep.content;
console.log(testResponse);
});
}
php (controller):
public function newListArticleAction()
{
if ($this->request->isPost()) {
if ($this->request->isAjax()) {
$receivedDatas = json_decode($this->request->getPost('param'));
$newSelArticleCode = $receivedDatas->selArticleCode;
$tarif = $receivedDatas->tarifClient;
if (($tarif == '') || (!$tarif)) {
$tarif = 1;
}
$tarifs = array();
$selectedArticle = Articles::findFirstByCode($newSelArticleCode);
if (!$selectedArticle) {
$this->view->messagesInfo = "erreur: article introuvable";
$this->jsonMessages['contenu'] = 'erreur';
} else {
$tarifs[0] = $selectedArticle->prixVente;
$tarifs[1] = $selectedArticle->prixLocation1;
$tarifs[2] = $selectedArticle->prixLocation2;
if( strcmp($receivedDatas->vl, "VENTE") == 0 ) {
$selectedArticle->tarif = $tarifs[0];
}
else {
$selectedArticle->tarif = $tarifs[$tarif];
}
$tarif = $selectedArticle->tarif;
$newListeArticle = new Listearticle();
$newListeArticle->tarifFinal = $selectedArticle->tarif;
$newListeArticle->articleID = $selectedArticle->id;
$newListeArticle->descriptionV = $selectedArticle->description;
$newListeArticle->designationV = $selectedArticle->designation;
$newListeArticle->stockVente = 0; //TODO
$newListeArticle->save();
$this->jsonMessages['content'] = "test message";
return $this->jsonMessages;
}
}
}
}
so, here $newListeArticle object is correctly saved in DB (postgres), but response.responseText is empty.
If I comment "$newListeArticle->save();" , then I get a response as expected
($this->jsonMessages is handled in ControllerBase )