Reputation: 515
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
Upvotes: 0
Views: 78
Reputation: 100175
you could create a global variable to store the url, in your html head
, so that any js file that needs the url can access it, like:
<html>
...
<head>
..
var MY_APP_URL = '<?php echo Yii::app()->baseUrl; ?>'; //can be like www.somesite.com/
....
//load js files
...
and in the js file you could do:
$.ajax({
url: MY_APP_URL + "controller_name/function_name",
data: {'type':tagtype},
OR you could do:
Yii::app()->clientScript->registerScript('helpers', '
someUrl = '.CJSON::encode(Yii::app()->createUrl('blabla')).';
baseUrl = '.CJSON::encode(Yii::app()->baseUrl).';
');?>
and you can use variables someUrl
and baseUrl
in your Javascript files
Upvotes: 4