从js文件调用laravel路由 [英] Call laravel route from js file
问题描述
我第一次使用Laravel 5,当刀片调用JS文件时,我有一个刀片,其中包含JS文件.无法识别网址: 这就是我在JS文件中调用我的URL的方式:
I'm working with Laravel 5 for the first time, i have a blade where i include a JS file, when the blade calls the JS file. It doesn't recognize the URL : this is how i call my URL in JS file :
$.ajax({
type: "POST",
cache: false,
url : "{{URL::to('zone')}}",
data: {'ma':$('select[name=ma]').val()},
success: function(data) {
...
}
});
当我在myBlade.blade.php中包含此代码时,它可以正常工作,但是从JS文件中我收到了403错误
When i include this code in myBlade.blade.php it works fine but from the JS file i got the 403 error
推荐答案
刀片不处理JavaScript文件,仅处理扩展名为blade.php
的文件
Blade doen't process JavaScript files, only those with blade.php
extension
解决方案可能是为全局配置对象提供您感兴趣的路由的集合.
Solution may be to provide a global configuration object with a collection of routes you are interested in.
假设您有两个单独的文件:index.blade.php
加main.js
Assuming you have two separate files: index.blade.php
plus main.js
1)index.blade.php
<script>
// global app configuration object
var config = {
routes: {
zone: "{{ URL::to('zone') }}"
}
};
</script>
<script src="main.js"></script>
2)main.js
$.ajax({
type: "POST",
cache: false,
url : config.routes.zone,
data: {'ma':$('select[name=ma]').val()},
success: function(data) {
...
}
});
这篇关于从js文件调用laravel路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!