Get base_url using Jquery in Laravel

后端 未结 3 731
孤城傲影
孤城傲影 2020-12-18 05:55

I am using laravel 5.0. I want to get base url of laravel page using jquery. I have tried the following function.

function getBaseURL () {
   return location         


        
相关标签:
3条回答
  • 2020-12-18 06:16

    You can also use this:

    <script type="text/javascript">
          var base_url = '{!! url() !!}';
    </script>
    

    You can call it any where in js files like "base_url"

    It return upto your public path without slash at end

    Ex: www.example.com

    If you need to add slash at end you can use this..

    <script type="text/javascript">
          var base_url = '{!! url().'/' !!}';
    </script>
    

    It return upto your public path with slash at end

    Ex: www.example.com/

    0 讨论(0)
  • 2020-12-18 06:25

    The solution is easy , All you need to just use below code :-

    var appBaseUrl = {''};
    

    This variable will return something like http://yourexampledomain.com

    Furthermore if you want to add your controllers and action to this url i.e. for Ajax call , then that will be simple too :-

    var finalUrl = appBaseUrl+'/controller/action';
    

    Result is :- http://yourexampledomain.com/controller/action

    Now use finalUrl in your calling route for ajax calls.

    0 讨论(0)
  • 2020-12-18 06:26

    Do you mean you want the base url of your laravel app available anywhere your javascript?

    You can include this in your template.blade.php:

    <script type="text/javascript">
    var APP_URL = {!! json_encode(url('/')) !!}
    </script>
    

    And than access that var anywhere in your javascript:

    console.log(APP_URL);
    
    0 讨论(0)
提交回复
热议问题