在Laravel中使用Jquery获取base_url [英] Get base_url using Jquery in Laravel

查看:288
本文介绍了在Laravel中使用Jquery获取base_url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5.0.我想使用jQuery获取laravel页面的基本URL. 我已经尝试了以下功能.

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.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";
}

但这仅给出http://localhost.我需要我的完整基本URL在jquery中.该怎么办?

But this gives only http://localhost. I need my full base url in jquery. What to do?

推荐答案

您的意思是您希望laravel应用的基本网址在您的JavaScript的任何位置都可用吗?

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

您可以将其包含在template.blade.php中:

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

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

然后在您的javascript中的任何位置访问该变量:

And than access that var anywhere in your javascript:

console.log(APP_URL);

这篇关于在Laravel中使用Jquery获取base_url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆