PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] ajax call controller in yii (javascript)

 January 09, 2022     ajax, javascript, jquery, php, yii     No comments   

Issue

For some reason this doesnt work and I can't find a way to make it work!

on my controller named "ExplicacaoController" I have this:

public function accessRules()
...
'actions'=>array('index','view', 'test', 'ajaxrequest'),
...
public function actionAjaxRequest()
    {
      $val1 = $_POST['val1'];
      $val2 = $_POST['val2'];
      echo "something";

      Yii::app()->end();
    }

On my view I have:

<script type="text/javascript">
...
$.ajax({
      type: "POST",
      url:    "<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>",
      data:  {val1:1,val2:2},
      success: function(msg){
           alert("Sucess")
          },
      error: function(xhr){
      alert("failure"+xhr.readyState+this.url)

      }
    });
...

What happens is that I allways get this error:

failure4<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>

I really need some help with this


Solution

Try to put

<? echo Yii::app()->createUrl('Explicacao/ajaxRequest'); ?>

instead of

<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>

The thing is in create url you need to put the Controller ID not the Controller full name.

If it's not working you could try both Explicacao/ajaxRequest or explicacao/ajaxRequest because your rout could be case sensitive depending on your conf



Answered By - darkheir
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing