【问题标题】:Use ajax/json in yii framework在 yii 框架中使用 ajax/json
【发布时间】:2015-01-26 22:40:49
【问题描述】:

我正在尝试在 Yii 框架中创建一个网上商店。现在我想在用户单击图标时取消设置会话。

我目前正在使用它向文件发送 json 调用,但我认为 json 调用中的 url 正在被我的 htaccess 重写。

JSON 调用:

$(document).ready(function(){
    $('.glyphicon-trash').click(function(){
        $.getJSON("ajax/load.php?action=unset&element="+$(this).parent(), function(data) {
            alert(data.message);
        });
    });
});

我得到的错误:

GET http://mydomain.nl/my/path/to/site/ajax/load 404 (Not Found)

但它不是加载,它的 load.php!但是我的 htaccess 重写了那个 url..

.htaccess

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

在 Yii 中实现 ajax 调用的其他方式也不错,只是需要我多解释一下。

【问题讨论】:

  • 当然.htaccess会重写。 CController::createUrl() 有定义的路线怎么样?在教程中很容易找到。
  • 嗨伙计,你为什么不这样传递 url:ajax/load/action/unset/element=...
  • @Deer-Outdoor.nl 实际网址是什么?我需要您如何在浏览器中访问该网址?
  • @Deer-Outdoor.nl 哥们,还不清楚:(我的意思是你可以直接访问的url

标签: php ajax json .htaccess yii


【解决方案1】:

您可以通过核心 yii-ajax 来实现这些,但如果您想使用 ajax 进行大量工作,那么您可以使用带有“renderPartial”ajax 和小部件的核心 YII 库。

这是 yii 的核心 ajax 调用

在您的视图中添加以下行。

<input type="hidden" id="baseUrl" value="<?php echo Yii::app()->getBaseUrl(true);?>">

在您的 js 文件中添加以下行。

function changeData(val1){

 var baseUrl = $('#baseUrl').val();
 var urlCI = baseUrl+'/Controller/actionMethod/;

 $.ajax({
        type: "GET",
        url: urlCI,
        data:{
            'action':'unset',
            'element' : 'someValue'
        },
        success: function(response) {
            if(response!=''){
                //do whatever work you want
                return true;
            }
            else{
                return false;
            }
        }
    });

}

【讨论】:

    【解决方案2】:

    好的,我现在开始工作了,我在站点视图下创建了一个文件。现在我这样称呼它:

    $(document).ready(function(){
        $('.glyphicon-trash').click(function(){
            var session = 'session';
            $.getJSON("<?php echo Yii::app()->baseurl; ?>/load?action=unset&session="+session, function(data) {
                location.reload(true); //<---- this one does not work tho...
            });
            setInterval(function(){location.reload(true);}, 500);
        });
    });
    

    【讨论】:

    • 我真的很惊讶这个问题花了这么长时间(6 小时)才得到答案。对我有用的替代方法是$.get("&lt;?=$this-&gt;createUrl('controller/action', array('action'=&gt;'unset'))?&gt;");
    【解决方案3】:

    您不得对网址进行硬编码。您必须按照@Grimv01k 的建议使用createUrl()createAbsoluteUrl()。在视图或控制器中时,您可以通过

    $this->createUrl( 'foo/bar',array('id'=>321) )
    

    其他方式:

    Yii::app()->controller->createUrl('/baz/foo/bar'); // will use current controller
    

    Yii::app()->createUrl('/baz/foo/bar')
    

    已编辑

    如果你愿意

    当用户点击图标时取消会话

    您可以通过以下方式做到这一点。

    echo CHtml::ajaxLink(
        'click me to destroy session, mua-ha-ha',
        Yii::app()->createUrl( '/session/destroy' ),
        array(
            'type' => 'post',
            'dataType' => 'json',
            'data' => array(
                'foo' => 'bar',
            ),
            'beforeSend' => 'js:function(){
                console.log( $(this) );
                return false;
            }',
            'success' => 'js:function(data){
                console.log(data);
            }',
        )
    );
    

    SessionController:

    public function actionDestroy () {
        if ( isset( $_POST['foo'] ) ) {
            Yii::app()->session->destroy();
            if ( Yii::app()->request->isAjaxRequest ) {
                $response = array(
                    'status' => !Yii::app()->session->sessionID
                ) ;
                echo CJSON::encode( (object) $response );
                Yii::app()->end();
            }
        }
    }
    

    【讨论】:

    • 好的,我如何将它与 jquery 一起使用?因为这就是你在那里写的所有 PHP..
    • 我不能用这个。我需要先设置一些 jquery 选项。就像我需要先从表中获取一些值,然后才能取消设置会话。所以你的选择可以工作,但如果你需要页面其他元素的文本,则不能。
    • 你错了。您可以在 PHP 中收集您的值并将它们放入 data 部分(在我的示例中 foo 和 bar 所在的位置)。如果 PHP 级别的数据不可用,您可以从 jQuery 收集数据。 ajaxLink 中的所有这些数组都是 AJAX options。所以你可以在那里添加beforeSend。我已经通过这部分更新了我的示例。现在在控制台中,您必须看到点击链接对象的转储。
    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多