简体   繁体   English

Yii框架,将jquery参数传递到Yii :: app()-> createUrl('controller / action',array('param'=>'value')

[英]Yii framework, pass jquery parameters into Yii::app()->createUrl('controller/action',array('param'=>'value')

I'm new in yii and I have this script in one of my views: 我是yii的新手,并且在我的一种观点中有此脚本:

$('#div_exams').on('click', 'a[id^="download"]', function(e) {

    e.preventDefault();

    var fileName = e.target.id;

    $.ajax({
        type: "POST",
        url: "<?php echo Yii::app()->createUrl("exams/downloadExam",array('fileName'=>'HERE I NEED TO PASS FILENAME')); ?>",

        success: function(jsonResponse) {


        },
        error: function() {

        }
    });

});

So my question is, how can I pass the var fileName in javascript to create the url in yii? 所以我的问题是,如何在javascript中传递var fileName以在yii中创建url?

Thank you very much :) 非常感谢你 :)

Option 1: Build a URL template in Yii and interpolate each needed filename into it: 选项1:在Yii中构建一个URL模板,并将每个所需的文件名插入其中:

var baseUrl = <?php echo json_encode(Yii::app()->createUrl("exams/downloadExam",array('fileName'=>'__PLACEHOLDER__'))); ?>; 

$('#div_exams').on('click', 'a[id^="download"]', function(e) {
  ...
  var url = baseUrl.replace('__PLACEHOLDER__', encodeURIComponent(filename));
  ...
}

Option 2, probably cleaner: Build each URL in Yii, attach it to the link as a data- attribute , read it using JS: 选项2,可能更干净:在Yii中构建每个URL,将其作为data-attribute附加到链接,使用JS读取它:

// when building each download link:

<?php echo CHtml::link('Download link, 'some url', array(
  'id' => 'download_1234',
  'data-download' => Yii::app()->createUrl("exams/downloadExam",array('fileName'=>'the file')),
));

// in the script

$('#div_exams').on('click', 'a[id^="download"]', function(e) {
  ...
  var url = $(this).data('download');
  ...
}

Note that neither solution abuses the id attribute like you're doing now. 请注意,没有一种解决方案会像您现在那样滥用id属性。

If I understand your problem correctly, you need to create an URL for downloading something with Javascript? 如果我正确理解了您的问题,则需要创建一个URL,以使用Javascript下载内容吗?

That is not possible. 这是不可能的。 JS does not work server side so you can not create an URL using PHP code inside the JS function once its loaded in the client (browser). JS在服务器端不起作用,因此一旦将其加载到客户端(浏览器)中,就无法在JS函数内部使用PHP代码创建URL。

You can however, have an action in Yii that you pass a value and it echoes a URL, you get that URL with Jquery (responseText I think) and use it whenever you want. 但是,您可以在Yii中执行一个操作,即您传递一个值并回显URL,然后使用Jquery(我认为是re​​sponseText)获得该URL,并在需要时使用它。

You could also pass the filename as POST data if you use the data option in jQuery.ajax() : 如果在jQuery.ajax()使用data选项,也可以将文件名作为POST数据传递:

$('#div_exams').on('click', 'a[id^="download"]', function(e) {

    e.preventDefault();

    var fileName = e.target.id;

    $.ajax({
        type: "POST",
        url: "<?php echo Yii::app()->createUrl("exams/downloadExam"); ?>",
        data: {fileName: fileName},
        success: function(jsonResponse) {


        },
        error: function() {

        }
    });

});

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

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