how to define Url-rule for rendering AJAX request?

Hi guys,

following JQuery statement will render a method in controller getting records by AJAX




 $.get('index.php?r=textbaustein/baustein',{textId:textId},function(data)...



Controller with method actionBaustein() is in folder frontend/controllers, View, which is calling method is in folder [size="2"]frontend\modules\mail\views\mail-ausgang[/size]

I assume , I have to initialize URL-rule in config in order to get AJAX-request. After having set prettyUrl TRUE, I will get 404 Error of AJAX.

Any ideas, how to define rule correctly. Notice, that no view will be rendered. Controllermethod just will give back JSON.

Thx in advance for every constructive help

Hi Thomas,

  1. enableStrictParsing may or may not affect this ajax call. It depends on the url rules that you currently have.

If you already have a rule that will be applied to the route “textbaustein/baustein” (textbaustein controller’s baustein action), then it will be OK.

http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html#$enableStrictParsing-detail

  1. showScriptName will affect this ajax call for sure. You’ll have to edit your url for the ajax call like the following:



$.get('/index.php/textbaustein/baustein',{textId:textId},function(data){



You may want to use Url::to() to create the ajax url and use it in the script:




$ajaxUrl = Url::to(['textbaustein/baustein']);

...

$.get('$ajaxUrl',{textId:textId},function(data){



This will work if ‘showScriptName’ is either true or false, or even if ‘enablePrettyUrl’ is false.

1 Like

valuable help!

  1. How to mark this post as solution?

  2. How to give reasonable posts reputation points?

1 Like
  1. I’m not sure. If you have a pencil icon behind the thread title, you can edit it. But in general you don’t need to mark the thread as “solved”, since other people may have something to add or ask further.
  2. You can click on the heart mark at the bottom of every post.
1 Like