CakePHP: Risposta Controller per Ajax richiesta è sbagliato
-
26-09-2019 - |
Domanda
sto usando jQuery per fare una richiesta AJAX per qualche azione di controllo. Questa richiesta viene attivato da un pulsante nella vista dell'azione di modifica nello stesso controller.
Il mio problema: L'Ajax richiesta sta tornando tutto il codice della vista di modifica (con tutte le forme e gli ingressi) al posto del numero previsto. Se metto lo stesso tasto ajax in vista dell'azione aggiuntivo, funziona perfetto (restituisce il numero).
La modificare e aggiungere le azioni resti come generato da default (con cottura).
Questa è la funzione jQuery per fare la richiesta Ajax
$.ajax({
type: 'POST',
url: 'checkTargets',
data: {target: tgt_array, channel: channel_type},
success:function(data){
$('#num_subscribers > span').html(data);
},
error:function(){
$('#num_subscribers > span').html("The subscribers could not be loaded");
},
timeout: 5000
});
}
Questa è l'azione
function checkTargets() {
if ($this->RequestHandler->isAjax()) {
if(!empty($this->params['form'])) {
$data = $this->params['form'];
if ($data['channel'] === 'SMS') {
$channel = 'sms';
} else {
$channel = 'pin';
}
$targets = $this->processPostTargets($data['target']);
$this->RequestHandler->respondAs('text');
//This echo a NUMBER
echo ClassRegistry::init('Selection')->countSubscribersInTarget($channel, $targets);
Configure:: write('debug', 0);
$this->autoRender = false;
exit();
}
}
}
Perché succede questo?
Grazie
Soluzione 2
I risolto il problema. E 'stato il parametro URL nella funzione .ajax (un errore niubbo)
Dovrebbe essere un percorso completo per l'azione si fa riferimento nel controller.
Questa domanda mi ha aiutato a capire il problema: migliori pratiche per l'uso azione URL durante la chiamata ajax in cakePHP
fissi Ajax Richiesta:
$.ajax({
type: 'POST',
url: '/myapp/campaigns/checkTargets',
data: {target: tgt_array, channel: channel_type},
success:function(data){
$('#num_subscribers > span').html(data);
},
error:function(){
$('#num_subscribers > span').html("The subscribers could not be loaded");
},
timeout: 5000
});
grazie e scusa il mio inglese
Altri suggerimenti
Solo un suggerimento senza avere accesso a tutto ciò, hai provato questo?
$this->autoRender = false;
$this->layout = 'ajax';
Anche io suggerirei mantenerlo semplice:
$.post("/controller/checkTargets", function(data) {
alert(data);
}
function checkTargets() {
$this->autoRender = false;
$this->layout = 'ajax';
echo "Im working";
}
e passare da lì.