3

i am trying to include laravel .env variable into my js file my file name is custom-script.js and my js code is:

     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',

and my .env file contain

 APP_URL=http://localhost:8000

how i can replace APP_URL=http://localhost:8000 into my url: 'http://localhost:8000/Process',

Your help will be highly appreciated

            $.ajax({
            type: 'post', //Method type
            url: '{{env("API_URL")}}/api/apikey?api_key
7
  • url : '{{env("APP_URL")}}/'+'Process'; Doesn't this work for you ? Commented Apr 26, 2018 at 5:47
  • its not working here Commented Apr 26, 2018 at 5:57
  • please see my question i have shared Commented Apr 26, 2018 at 5:58
  • url: '{{env("API_URL")}}'+'/process'+'/api/apikey?api_key' You need to concatenate process Commented Apr 26, 2018 at 6:00
  • forget about the old url Commented Apr 26, 2018 at 6:01

2 Answers 2

5

In your blade file:

<script>
    var action = "{{ env("APP_URL") }}"
</script>

In your js:

url : action+'/process',
Sign up to request clarification or add additional context in comments.

5 Comments

please see my eidt question
Actually, what are you want to do?
$.ajax({ type: 'post', //Method type url: '127.0.0.1:8000/api/apikey?api_key='+ connectApi +'&token
i want to replace 127.0.0.1:8000 with API_URL=127.0.0.1:8000
this is a bad practice
1

Here is an another alternative.

Delete your custom-script.js file from your public directory

In your Route (web.php)

Route::get('/custom-script.js','ScriptController@index');

In your ScriptController

class ScriptController extends Controller
{

    public function index(){
        return response()->view('custom-script')->header('Content-Type','application/javascript');
    }

}

Now put your js code in custom-script.blade.php file

alert('{{ env("APP_URL") }}');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.