gpt4 book ai didi

jquery - ajax无法通过YII2后传递数据

转载 作者:行者123 更新时间:2023-12-01 05:36:52 26 4
gpt4 key购买 nike

我在 View 中有一个正常的表单。我没有使用事件表单,因为在某些情况下我使用的是 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;
});

技术人员 Controller :

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

在 Controller 中,我转储 $_POST 以检查 ajax 是否已成功传递到 Controller 上,但没有任何反应。这和csrf有关系吗?因为这是我不久前的问题,但现在是这个。

最佳答案

你的问题是:

           type: 'get',

设置为

           type: 'post',

关于jquery - ajax无法通过YII2后传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com