簡體   English   中英

Ajax無法通過YII2傳遞數據

[英]ajax cannot pass data through post YII2

我認為該表格具有正常形式。 由於某些情況,我不使用活動表單,因為我在使用jquery子窗口並且活動表單在appAsset中有沖突。

<form id="technician-create-form" method="post">
    <input type="hidden" name="csrf" id="csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />                                           
    <div class="col-md-6">
        <div class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-4 control-label">Technician No:</label>
                <div class="col-sm-8">
                    <input type="text" name="technician_no" id="technician_no"  class="form-control" placeholder="">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">Code:</label>
                <div class="col-sm-8">
                    <input class="form-control" name="tech_code" id="tech_code">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">Name:</label>
                <div class="col-sm-8">
                    <input type="text" name="tech_name" id="tech_name" class="form-control" placeholder="">
                </div>
            </div>
        </div>
    </div>

    <div class="col-sm-offset-4 col-sm-9">
        <input type="button" value="Save" id="addTechnicians" class="saveNew" name="addNewTechnicians">
        <button type="reset" class="btn btn-default">Reset</button>
    </div>

</form>

jQuery的

$(".saveNew").on("click",function(){
    //console.log("ok");
    var myBtn = this.name;
    switch (myBtn){
        case "addNewTechnicians":
            console.log(myBtn);
            var _csrf = $("#csrf").val();
            var technicianNo = $("#technician_no").val(),
                techCode = $("#tech_code").val(),
                techName = $("#tech_name").val(),

            $.ajax({
                url: 'technicians/create',
                type: 'post',
                data: 
                    {
                        _csrf : _csrf,
                        //_csrf : csrfToken,
                        technicianNo : technicianNo,
                        techCode : techCode,
                        techName : techName,

                    },
                //dataType: json,
                success: function(result){
                    var res = JSON.parse(result);
                    notificationMessage(res.technicians.notification,res.technicians.template);
                    if(res.technicians.template == "success"){
                        // $("#technician_no").val("");
                        // $("#tech_code").val("");
                    }

                },
                error: function(result){
                    console.log(result);
                    alert("error in ajax form submission");
                    },
            });
     break;
 });

技術員控制器:

public function actionCreate()
{
    var_dump($_POST);
}

在控制器中,我正在轉儲$ _POST,以檢查ajax是否已成功通過控制器,但沒有任何反應。 這與csrf有關嗎? 因為那是我前一段時間的問題,但是現在,這是我的問題。

你有問題:

           type: 'get',

設置為

           type: 'post',

暫無
暫無

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

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