My application is deployed in a URL prefixed with "http://hostname/baseurl/". The "/baseurl" part is environment-dependent, and varies from server to server. Now I need to make ajax call in separate js file. I need to pass the prefix to the js file so that the ajax URL is portable. How to pass the PHP variable to the Javascript code? Is there a solution in Yii?
Thank you zaccaria, that’s the easy part. Hard part is the interoperability of Javascript.
Thank you yodel. It is working! Pretty elegant.
Also need to note that if this variable is going to be used in a $(document).ready() function, it must be loaded before it, say CClientScript::POS_HEAD or CClientScript::POS_BEGIN.
ok, javascript files are not support the related path in MVC, if you want to use any php code, like the baseUrl , in your javascript file, do the following, in your main layout file, in the head section, add-
<script>
var baseurl="<?php print Yii::app()->request->baseUrl;?>";
</script>
and now you can use this baeurl in your javascript file, enjoy