简体   繁体   English

如何在yii中使用Ajax和JQuery

[英]How to use Ajax , JQuery in yii

I am using Ajax and jQuery in yii. 我在yii中使用Ajax和jQuery。

I have a view its url is like that. 我认为它的网址就是这样。

http://localhost/UnderstandQuran/index.php?r=Verse/topicverse&topic_id=1

Now there are some radio buttons group. 现在有一些单选按钮组。 When i click on it, it will be called by jquery. 当我单击它时,它将被jquery调用。

My View Code. 我的查看代码。

<?php $i= 0;
foreach($data->verse_lang as $vtlanguage) {
$i = $i+1 ;
if($i === 1) { ?>
<input type="radio" name="<?php echo "tran".$data->id ?>" class="translanguage" langid="<?php echo $vtlanguage['id']; ?>" verseid="<?php echo $data->id ?>" checked="checked"><?php echo $vtlanguage['language_name']; ?></input>
<?php } else { ?>
<input type="radio" name="<?php echo "tran".$data->id ?>" class="translanguage" langid="<?php echo $vtlanguage['id']; ?>" verseid="<?php echo $data->id ?>"><?php echo $vtlanguage['language_name']; ?></input>
<?php } } ?>

My JQuery Code 我的JQuery代码

$("input[type='radio'][class='translanguage']").click(function ()
{
    var siteUrl = document.URL;
    var verseid = $(this).attr("verseid");
    var langid = $(this).attr("langid");
    var vname = $(this).attr("name");

    $.ajax(
    {
        type: "POST",
        url: siteUrl + "/singleverse",
        data:
        {
            verseid: verseid,
            langid: langid
        },
        success: function (result)
        {
            alert(result);
        }
    });
});

Through Ajax, i am sending it to the controller/Controller Method. 通过Ajax,我将其发送到控制器/控制器方法。

My Controller Method 我的控制器方法

public function actionSingleverse()
{
    $verseid = $_POST['verseid'];
    $langid = $_POST['langid'];
    echo $verseid." ".$langid;
}

My Question 我的问题

Now through all of my code. 现在通过我所有的代码。 It should be giving the alert box with two different integer value, but it is displaying all the html code of the page. 它应该为警告框提供两个不同的整数值,但是它显示页面的所有html代码。 So how i can solve this issue. 那么我如何解决这个问题。 I have checked in console.log, but gives 505 error. 我已经检查了console.log,但显示505错误。

My all this code is working fine when the above URL is like that. 当上面的URL像这样时,我的所有代码都工作正常。

http://localhost/UnderstandQuran/index.php?r=Verse

Please help me, what and where is the problem. 请帮助我,问题出在哪里。

Thanks. 谢谢。

以以下格式粘贴您的网址,确保可以正常使用

  url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/controllername/singleverse',

May be there will be some problem in this place 可能这个地方会有问题

var siteUrl = document.URL;

The Site Url can also be got in javascript like this 网站网址也可以像这样在javascript中获取

var siteUrl = <?php echo Yii::app()->getBaseUrl() ?>;

May be this could help you 也许这可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM