sendPayload nevraci JSON, ale HTML cele stranky
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- kudlajz
- Člen | 70
Zdravim, mam implementovane odesilani nekterych formularu pres AJAX a jako
odpoved by se mel vracet JSON.
Napr. zde kod z presenteru:
if (!$user) {
$this->payload->error = true;
$this->payload->response = $this->template->translate("There is no user registered with this email address.");
}
$this->sendPayload();
Javascript
if (Nette.validateForm(document.getElementById(formID))) {
$.post(form.action, $(form).serialize(), function(data) {
if (data.error) {
createBubble(data.response, "error");
} else {
createBubble(data.response, "success");
$(form).closest(".modal").modal("hide");
}
});
}
Avsak namisto payload vraci celou stranku v HTML. Kdyz jsem to posledne testoval, tak to fungovalo vsechno spravne a ted to zas nefunguje a to jsem v tom nic nemenil. Nejake rady? Dekuji.
Editoval kudlajz (20. 4. 2013 14:20)
- Jan Tvrdík
- Nette guru | 2595
Je nejaky zpusob jak to poznat?
Co třeba dát tam breakpoint pustit debugger? Nebo aspoň si před ní a po ní zkus něco dumpnout.
- kudlajz
- Člen | 70
Nepomohlo nic, zkousel jsem pred i po volani $this->sendPayload() umistit Debugger::log, ale ani ten nic nezapise, takze je pravdepodobne, ze se ta metoda vubec nevola, otazka je ale proc? Cela handle je zde:
public function submitLostPassword(Form $form)
{
try {
$data = $form->values;
$user = $this->usersModel->getByEmail($data["email"]);
if (!$user) {
$this->payload->error = true;
$this->payload->response = $this->template->translate("There is no user registered with this email address.");
} else {
if ($user->facebook_id) {
$this->payload->error = true;
$this->payload->response = $this->template->translate("There is no user registered with this email address.");
} else {
$password = $this->authenticator->generateRandomPassword();
$passwordHashed = $this->authenticator->calculateHash($password);
$this->usersModel->update($user->id, array("password" => $passwordHashed));
$template = $this->createTemplate();
$template->setFile(APP_DIR . "/templates/Emails/lostPassword.latte");
$template->data = $user;
$template->password = $password;
$mail = new \Nette\Mail\Message();
$mail->setMailer($this->mailer);
$mail->setFrom("noreply@mygidoo.com", "MyGidoo");
$mail->addTo($user->email);
$mail->setHtmlBody($template);
$mail->send();
$this->payload->error = false;
$this->payload->response = $this->template->translate("We have sent you an email with your new password.");
}
}
}
catch (\DibiException $e) {
$this->setAjaxError();
}
$this->sendPayload();
}