簡體   English   中英

在jquery模式窗體對話框中獲取ajax響應

[英]get ajax response in jquery modal form dialog

提交模態對話框表單時,我無法獲得ajax響應。 當表單不是模態時,它可以完美工作。

表格:

<div id="form2" style="display: none">
    <form id="objectInsert" action="element/create" method="POST" enctype="multipart/form-data">  
        <div class="form-group">
            <label for="name">Name</label>
            <input class="form-control" type="text" name="name" id="name"/>
        </div>

        <div class="form-group">
            <label for="description">Description</label>
            <textarea class="form-control" name="description"></textarea> 
        </div>
    </form>

在這里,我在控制台中獲得了ajax成功部分!

$("#objectInsert").submit(function(e) {
    e.preventDefault();
    resetErrors();
    var form = this;
    var url = $(this).attr('action');
    var data = new FormData($(this)[0]);
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        dataType: 'json',
        cahe:false,
        processData: false,
        contentType: false,
        success: function(resp) {
            console.log(resp);//Working
        },
        error: function() {
            console.log('there was a problem checking the fields');
        }
    });
});

在這里,我在控制台中獲得了ajax錯誤部分! 有人可以告訴我我做錯了什么嗎?

    $("#add_element").click(function(){
        $("#form2").dialog({
            modal:true,
            width:400,
            buttons:{
                Send:function(e){
                    e.preventDefault();
                    var form = $("#objectInsert");
                    var url = form.attr('action');
                    var data = new FormData(form[0]);
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: data,
                        dataType: 'json',
                        cahe:false,
                        processData: false,
                        contentType: false,
                        success: function(resp) {
                            console.log(resp);//not working
                        },
                        error: function(xhr, status, error) {
                            console.log('there was a problem checking  the fields');
                            console.log(xhr);
                            console.log(error);
                        }
                    });
                    return false;

                },
                Cancel:function(){
                    $(this).dialog("close");
                }
            }
        });
    });

控制器

public function create() {
    try{
        $this->form = new Form();
        $this->form->post('name');
        $this->form->val('isEmpty', 'name');
        $this->form->post('description');
        $this->form->val('isEmpty', 'description');

        $this->form->fetch();
        $this->form->submit();

        $data = $this->form->get_postData();

        $this->model->insert($data);

        echo json_encode('success');
    } catch (Exception $ex) {
        $errors = $this->form->get_error();
        $_SESSION["errors"] = $errors;
        //This is for ajax requests:
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&  
                strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) 
                == 'xmlhttprequest') {
            echo json_encode($_SESSION['errors']);
            exit;
        }

        foreach ($_SESSION["errors"] as $errors){
            echo $errors;
            echo '<br/>';
        }exit;
    }
}

看到此代碼,您尚未關閉功能塊

success: function(resp) {
         console.log(resp);//not working
       },//This is not closed for success function
error: function() {
         console.log('there was a problem checking the fields');
       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM