【问题标题】:Problem with sending requests to Controller by Ajax通过 Ajax 向 Controller 发送请求的问题
【发布时间】:2020-04-12 18:40:07
【问题描述】:

我的联系人视图中的验证存在问题。验证在我的控制器中,我正在尝试通过 AJAX 向那里发送我的表单数据。这就是我的 AJAX 代码的样子:

$(document).ready(function(){
    $('.contact_form').submit(function(event){
        event.preventDefault();
        let name = $('#name').val();
        let title = $('#title').val();
        let mail = $('#mail').val();
        let text = $('#text').val();
        $.ajax({ 
            type:'POST',
            url:'../Strona/contact',
            dataType:'json',
            crossDomain:false, 
            cashe:false, 
            contentType:"text/javascript",
            data:{
                name: name,
                title: title,
                mail: mail,
                text: text
             }
        })
        .done(function(data){
            console.log(data);
            if(data.res === 'yes'){
                console.log(data);
                $('.contact_form').html(data.list);
            }else{
                $.each(data.list,function(k,v){
                    console.log(data);
                    $('.error_'+k).append(v);
                });
            }
        });
    });

在我的控制器中进行验证:

if($this->request->is('post')){
    $data = $this->data;

    $errors=array();

    if(!$data['Contact']['name']){$errors['name'] = 'Bitte geben Sie einen Namen ein';}
    if(!$data['Contact']['email']){$errors['email'] = 'Bitte geben Sie eine E-Mail-Adresse ein';}
    if(!$data['Contact']['title']){$errors['title'] = 'Bitte geben Sie einen Titel ein';}
    if(!$data['Contact']['description']){$errors['description'] = 'Bitte geben Sie eine Nachricht ein';}
    if(count($errors) > 0){
        $result = array('res'=>'no','list'=>$errors);
        $el['Result'] = $result;
        $el['Contact'] = $data;
        $el['Contact'] = $el['Contact']['Contact'];
        $this->set('errors');
        $this->set('_serialize', array('errors'));
        $this->set('el',$el);
    }else{
        $result = array('res'=>'yes','list'=>'Vielen Dank für das Senden einer E-Mail');
        $txt = $data['Contact']['name'].'<br>'.$data['Contact']['description'];
        $subject = $data['Contact']['title'];
        $from = $data['Contact']['email'];

        $this->Ajax->mailsend($txt, $subject, $from);
        $data['result'] = $result;
        $this->Contact->save($data);
        $this->Session->setFlash('Nachricht gesendet');
        $this->redirect(array('action' => 'contact/'));
    }
}

当我单击按钮发送消息并且输入为空时,应显示错误,但没有任何反应。请求未发送到控制器。

我想问题可能出在我的wiev上。

<form method="POST" action="" class="contact_form">
    <div class="form-group">
        <input type="text" class="form-control" id="name" name="data[Contact][name]" value="<?php echo $el['Contact']['name']; ?>" placeholder="Dein Name"/>
        <div class="error error_name"></div>
    </div>
    <div class="form-group">
        <input type="email" class="form-control" id="email" aria-describedby="emailHelp" name="data[Contact][email]" value="<?php echo $el['Contact']['email']; ?>" placeholder="Deine E-Mail-Adresse"/>
        <div class="error error_email"></div>
    </div>
    <div class="form-group">
        <input type="text" class="form-control" id="title" name="data[Contact][title]" value="<?php echo $el['Contact']['title']; ?>" placeholder="Betreff"/>
        <div class="error error_title"></div>
    </div>
    <div class="form-group">
        <textarea class="form-control" id="describe" name="data[Contact][description]" placeholder="Deine Nachricht" rows="10"><?php echo $el['Contact']['description']; ?></textarea>
        <div class="error error_description"></div>
    </div>
    <input type="submit" id="send" class="btn btn-primary" value="Senden">
</form>

【问题讨论】:

  • 这能回答你的问题吗? onclick form send via ajax no page refresh
  • 一点点。我尝试使用这些建议并更改我的代码。我已经编辑了上面的代码。现在提交后查看不要刷新自己,但我仍然对ajax有问题。他没有开始。 cosole.log 没有任何想法。
  • 检查浏览器中的网络监视器,看看请求会发生什么。这应该会缩小问题所在。
  • 好的。我正在努力。网络给了我一些答案。控制器不会从表单中获取数据。但我不知道为什么。我会试着用这个做点什么。
  • 也许问题出在我的wiev上。我在上面添加。

标签: javascript php ajax cakephp


【解决方案1】:

您正在使用$this-&gt;set('el',$el);,但这只会使$el 变量可用于可能呈现的任何视图。假设您没有特定于 Ajax 的视图,它可能会退回到默认的 Ajax 视图,我认为它只是转储您设置为序列化的任何变量的内容。您还没有设置任何要序列化的内容。请参阅 JSON and XML views 了解如何设置 _serialized 变量,如果您想这样做的话。

如果您想更明确,可以替换 set 调用,而不是 send a string 作为响应:

$this->response->body(json_encode($el));
return $this->response;

【讨论】:

  • 我试图用这个 _serialized 变量做一些事情,就像在 CakeBook 中写的一样,但仍然不起作用。我不知道该怎么做。
【解决方案2】:

我还不能添加cmets,所以我会从我的角度回答。 AJAX 事件似乎被封装在一个函数中。如果您只想监听事件,请尝试将提交操作从函数中取出并放入$(document).ready();block,它应该会触发。我还建议您使用 JavaScript 而不是控制器进行表单验证。发回你的结果!希望这会有所帮助

【讨论】:

  • 我像你说的那样更改了我的代码,但验证仍在控制器中。现在,当我单击提交时,什么都没有发生。
猜你喜欢
  • 1970-01-01
  • 2012-04-07
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 2019-04-18
  • 2017-02-06
  • 1970-01-01
相关资源
最近更新 更多