【发布时间】:2011-11-30 12:33:05
【问题描述】:
我有一个表单,当用户提交这个表单时,它应该将数据传递给使用 AJAX 的函数。然后,结果会在对话框中显示给用户。我正在使用 CakePHP (1.3) 和 jQuery 来尝试实现这一点,但我觉得我遇到了困难。
该表单最终将用于上传带有标签的图像,但现在我只想看到一个消息在框中弹出..
形式:
<?php
echo $this->Form->create('Image', array('type' => 'file', 'controller' => 'images',
'action' => 'upload', 'method' => 'post'));
echo $this->Form->input('Wallpaper', array('type' => 'file'));
echo $this->Form->input('Tags');
echo $this->Form->end('Upload!');
?>
AJAX:
$(document).ready(function() {
$("#ImageUploadForm").submit(function() {
$.ajax({
type: "POST", url: "/images/upload/",
data: $(this).serialize(),
async: false,
success: function(html){
$("#dialog-modal").dialog({
$("#dialog-modal").append("<p>"+html+"</p>");
height: 140,
modal: true,
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
})
}
});
return false;
});
});
注意:如果我将$("#dialog-modal").dialog({ height: 140, modal: true }); 放在$.ajax 之外但在$("#ImageUploadForm").submit(function() { 内并注释掉$.ajax 的东西,我会看到一个对话框弹出,然后我必须单击它才能看到离开。之后就不会转发到/images/upload/的位置了
AJAX调用的方法:
public function upload()
{
$this->autoRender = false;
if ($this->RequestHandler->isAjax())
{
echo 'Hi!';
exit();
}
}
$this->RequestHandler->isAjax() 似乎什么都不做,或者它总是返回 false。我从来没有输入过以此为条件的 if 语句。
感谢所有帮助,如果您需要更多信息,请告诉我。
【问题讨论】:
标签: ajax jquery-ui cakephp jquery