[英]How to include laravel ..env variable into js file
我試圖將laravel .env變量包含到我的js文件中,我的文件名為custom-script.js,我的js代碼為:
function sendBusinessDetails(postForm) {
$.ajax({//Process the form using $.ajax()
type: 'post', //Method type
url: 'http://localhost:8000/Process', //Your form processing file URL
data: postForm, //Forms name
dataType: 'json',
和我的.env文件包含
APP_URL=http://localhost:8000
我如何將APP_URL = http:// localhost:8000替換為我的url:' http:// localhost:8000 / Process ',
非常感謝您的幫助
$.ajax({
type: 'post', //Method type
url: '{{env("API_URL")}}/api/apikey?api_key
在blade
文件中:
<script>
var action = "{{ env("APP_URL") }}"
</script>
在您的js
:
url : action+'/process',
這是另一種選擇。
從公共目錄刪除您的custom-script.js文件
在您的路線 (web.php)
Route::get('/custom-script.js','ScriptController@index');
在您的ScriptController中
class ScriptController extends Controller
{
public function index(){
return response()->view('custom-script')->header('Content-Type','application/javascript');
}
}
現在將您的js代碼放入custom-script.blade.php文件中
alert('{{ env("APP_URL") }}');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.