简体   繁体   中英

print alert box in response to a ajax call in yii2

I have this jquery code in my view file

$('#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);
  }
})

from here the value the of priority and impact id is sent to the controller/ajax function

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

I am not able to even echo something in response here don't know whats wrong here any help would be appreciated.

response 在此处输入图片说明

You are mixing POST , GET and REQUEST

in ajax you use a POST but don't send nothins as POST param instead you pass param in url as GET params and in action you look for REQUEST but not for GET (or post)

And you access directly to the $_REQUEST instead of using yii2 method for this

You should rethink you code ..

anyway just as a first step looking to your ajax call you could use the param you pass as 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';
      }
  }

and in client post

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

在“如果”条件下也尝试Echo,并从网络控制台共享“响应”(共页)。

Sending javascript code from the PHP server to js isn't a good practice. What you are doing is essentially making a call to the server and sending it the data and not doing anything with the resposne you've received.

Try to create a proper callback function like shown in this example,

Add a callback function (AJAX is Asynchronous, so your return is being hit before there is any data to return):

function returnData(param) {
  console.log(param);
}

Now add that callback function as a parameter to your AJAX function, and lets run it:

function getCartProduct(id, callback){
  $.post('sever.php', 
    function(data){
        callback(data);
    });
}

getCartProduct(id, returnData);

Also the server's response is treated as a string in javascript. To evaluate it as a javascript syntax, pass the string into the eval() method.

eval('alert("this works");');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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