1

I am trying to make simple ajax :

$('.send-contact-button').on('click', function(e){
        e.preventDefault();

       var id = $('.send-contact-button').data('id');
       var flag = $('.send-contact-button').data('flag');

       $.ajax({
           method : 'GET',
           url : "contactHandler?id="+id+"&flag="+flag,
           success : function( data ){
                console.log( data );
           }
       });

    });

but every time i get the html content. Tryied to send the data through json but without result. This Ajax is just test. Controller:

public function contactHandler($id, $flag)
{
    echo $id;
}

Can you guys tell me where i am wrong ? Thank you very much!

Toma Tomov
  • 1,476
  • 19
  • 55

2 Answers2

1

Your action is not defined correctly, you are probably getting an error page.

try this:

public function actionContactHandler($id, $flag)
{
    echo $id;
}
Ed209
  • 821
  • 5
  • 8
1

You ajax call should be formatted using yii2 urlmanager rules (contact-handler instead of contactHandler

  $.ajax({
       method : 'GET',
       url : "contact-handler?id="+id+"&flag="+flag,
       success : function( data ){
            console.log( data );
       }
   });

and in your controller you must add the action text to your function name
eg:public function actionContactHandler($id, $flag)

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107