0

I am developing an application in Yii.. One issue i am facing is, when i am writing ajax functions, the url's will be in php. so i am unable to move the codes to another javascript file.

$.ajax({
             url: "<?php echo Yii::app()->createUrl('provider/ajaxdetail');?>",
             data: {'type':tagtype},
             beforeSend: function() { },
             success: function(data) {

                        }                   

             });

so what is the best method to move php out of the javascript so that i can move the javascript code to another js file. I Thought of putting the url's in a hidden box and then call it in the javascript function. but i hope there will be better methods to do this .

please help

4

1 回答 1

4

您可以在 html 中创建一个全局变量来存储 url,head以便任何需要该 url 的 js 文件都可以访问它,例如:

<html>
...
<head>
..
var MY_APP_URL = '<?php echo Yii::app()->baseUrl; ?>'; //can be like www.somesite.com/
....
//load js files
...

在 js 文件中你可以这样做:

$.ajax({
  url: MY_APP_URL + "controller_name/function_name",
   data: {'type':tagtype},

或者你可以这样做:

Yii::app()->clientScript->registerScript('helpers', '
        someUrl = '.CJSON::encode(Yii::app()->createUrl('blabla')).';
        baseUrl = '.CJSON::encode(Yii::app()->baseUrl).';
');?>

你可以在你的Javascript文件someUrl中使用变量baseUrl

于 2013-09-15T05:01:09.477 回答