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

查看:16
本文介绍了在 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.我需要我在 jquery 中的完整基本网址.怎么办?

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

推荐答案

你的意思是你想让你的 Laravel 应用的基本 url 在你的 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天全站免登陆