简体   繁体   English

通过 Ajax 向 Controller 发送请求时出现问题

[英]Problem with sending requests to Controller by Ajax

I have a problem with validation in my contact view.我的联系人视图中的验证存在问题。 The validation is in my Controller and I'm trying to sending there my form data through AJAX.验证在我的 Controller 中,我正在尝试通过 AJAX 将我的表单数据发送到那里。 This is how it looks like my AJAX code:这就是我的 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);
                });
            }
        });
    });

And validation in my Controller:并在我的 Controller 中进行验证:

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/'));
    }
}

When i click the button to send message and there is empty input an error should show, but there's nothing happening.当我单击按钮发送消息并且输入为空时,应显示错误,但没有任何反应。 Request was not send to the Controller.请求未发送到 Controller。

I think maybe the problem is in my wiev.我想也许问题出在我的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>

You're using $this->set('el',$el);您正在使用$this->set('el',$el); , but that just makes the $el variable available to whatever view might get rendered. ,但这只是使$el变量可用于可能呈现的任何视图。 Assuming that you don't have an Ajax-specific view, it may be falling back to the default Ajax view, which I think just dumps the contents of any variables that you set to be serialized.假设您没有特定于 Ajax 的视图,它可能会退回到默认的 Ajax 视图,我认为它只是转储您设置为序列化的任何变量的内容。 You haven't set anything to be serialized.您还没有设置任何要序列化的内容。 See JSON and XML views for how to set the _serialized variable, if that's how you want to go.有关如何设置_serialized变量的信息,请参阅JSON 和 XML 视图,如果您希望这样设置 go。

If you want to be more explicit, you could replace that set call and instead send a string for the response:如果你想更明确,你可以替换那个set调用,而是发送一个字符串作为响应:

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

I can't yet add comments, so I'll answer from my perspective.我还不能添加评论,所以我会从我的角度回答。 The AJAX event seems to be encapsulated in a function. AJAX 事件似乎被封装在 function 中。 If you want just to listen for the event, try taking the submit action out of the function and into a $(document).ready();如果您只想监听事件,请尝试将提交操作从 function 中取出并放入$(document).ready(); block and it should fire.块,它应该开火。 I also suggest you do your form validation in JavaScript instead of the controller.我还建议您在 JavaScript 而不是 controller 中进行表单验证。 Post back your results!发回你的结果! Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM