gpt4 book ai didi

javascript - 打印警报框以响应 yii2 中的 ajax 调用

转载 作者:行者123 更新时间:2023-12-03 03:49:52 25 4
gpt4 key购买 nike

我的 View 文件中有这个 jquery 代码

$('#ticket-ticket_impact_id').change(function() {

var priority = $('#ticket-ticket_priority_id :selected').val();
var impact = $('#ticket-ticket_impact_id :selected').val();
if ($('#ticket-ticket_priority_id').val() == '' || $('#ticket-ticket_impact_id').val() == '') {

} else {
$.post('index.php?r=support/ticket/ajax-ticket-sla&ticket_priority_id=' + priority + '&ticket_impact_id=' + impact);
}
})

$('#ticket-ticket_priority_id').change(function() {

var priority = $('#ticket-ticket_priority_id :selected').val();
var impact = $('#ticket-ticket_impact_id :selected').val();
if ($('#ticket-ticket_priority_id').val() == '' || $('#ticket-ticket_impact_id').val() == '') {

} else {
$.post('index.php?r=support/ticket/ajax-ticket-sla&ticket_priority_id=' + priority + '&ticket_impact_id=' + impact);
}
})

从这里,优先级和影响ID的值被发送到 Controller /ajax函数

public function actionAjaxTicketSla(){
$ticket_priority_id=$_REQUEST['ticket_priority_id'];
//var_dump($ticket_priority_id);die();
$ticket_impact_id=$_REQUEST['ticket_impact_id'];
if(Sla::find()->where(['ticket_priority_id'=>$ticket_priority_id,'ticket_impact_id'=>$ticket_impact_id])->exists())
{

} else{
echo '<script type="text/javascript">alert("No sla defined!");</script>';
}
}

我什至无法在这里回应某些内容,不知道这里出了什么问题,任何帮助将不胜感激。

回应 enter image description here

最佳答案

您正在混合 POST 、 GET 和 REQUEST

在 ajax 中,您使用 POST 但不发送任何内容作为 POST 参数相反,您将 url 中的参数作为 GET 参数传递在实际操作中,您会查找 REQUEST,但不会查找 GET(或 post)

并且您可以直接访问 $_REQUEST 而不是使用 yii2 方法来实现此目的

你应该重新考虑你的代码..

无论如何,这只是第一步查看您的 ajax 调用,您可以使用您传递的参数作为 get param

  public function actionAjaxTicketSla(){
$request = Yii::$app->request;
$get = $request->get();
$ticket_priority_id=$get['ticket_priority_id'];
//var_dump($ticket_priority_id);die();
$ticket_impact_id=$get['ticket_impact_id'];
if(Sla::find()->where(['ticket_priority_id'=>$ticket_priority_id,'ticket_impact_id'=>$ticket_impact_id])->exists())
{
echo 'OK';
} else{
echo 'No sla defined';
}
}

以及在客户帖子中

   $.post('index.php?r=support/ticket/ajax-ticket-sla&ticket_priority_id=' + 
priority + '&ticket_impact_id=' + impact,
function(data){
if (data !='OK') {
alert(data);
}
});

关于javascript - 打印警报框以响应 yii2 中的 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45212956/

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