Afser2000
Afser2000

Reputation: 163

AJAX queryString mismatch in YII

I have a YII view with an AJAX script in the page

        <script>
            $(".tasks-add").click(function(){
                $.ajax({
                    type: 'get',
                    url: '<?php echo $this->createUrl('field')?>',
                    data: {
                        index: 1
                          },
                    });
                });
        </script>

Now this JS is making a call in the normal URL style(http://myapp/task/field?index=1) though I have made use of YII urlManager to optimize for the 'path' style of URLs using:

'urlManager'=>array(
              'urlFormat'=>'path',
              'showScriptName'=>false,

I am getting a 'Bad request error' because the URL requested by AJAX is the old ugly ?arg=value1 style. Is there a way possible to make the AJAX call URL not have the query string like ?index=1 but like http://myapp/task/field/index/1

Upvotes: 0

Views: 311

Answers (3)

dInGd0nG
dInGd0nG

Reputation: 4114

The YII way

<script>
        $(".tasks-add").click(function(){
            $.ajax({
                type: 'get',
                url: '<?php echo $this->createUrl('field',array('index'=>1));?>'
            });
    </script>

OR

<script>
        $(".tasks-add").click(function(){
            <?php echo CHtml::ajax(array(
                 'url'=>array('field','index'=>1),
                 'type'=>'get',
              ));?>
            });
    </script>

Upvotes: 2

Brad M
Brad M

Reputation: 7898

Just concatenate the URL string. jQuery ajax by default will append query string parameters when using GET and including data.

Upvotes: 1

Musa
Musa

Reputation: 97717

Yes. do it manually.

    <script>
        $(".tasks-add").click(function(){
            $.ajax({
                type: 'get',
                url: '<?php echo $this->createUrl('field')?>'+'/index/1'
            });
    </script>

Upvotes: 1

Related Questions