我正在为我在 Laravel 中构建的网站构建一个商店定位器。由于 Blade 文件调用位于assests 文件夹中的js 文件。它无法识别这样的 URL
$.ajax({
url: '{{ URL::action('getLocation') }}',
// ...
});
这就是我的 route.php
Route::post('/getLocation', array('as'=>'getLocation','uses'=>'FrontController@getLocation'));
所以它没有找到文件。如何在 ajax URL 中调用这个函数?
最佳答案
这是我如何实现这一目标的演示
I might be late here. This is just a sample code to help understand people who visit this question. Hope this helps anyone who visits here.
在我的 路线.php 我定义了一个命名路由
Route::post('getLocation',array(
'as'=>'getLocation','uses'=>'FrontController@getLocation')
);
在我的 中添加了名称路由作为数据 url somehtmlform.blade.php 文件
{!! Form::open() !!}
{!! Form::text('input-name',null,array('class'=>'form-control search-input','data-url'=> URL::route("getLocation") ))
{!! Form::close() !!}
我的 search.js 文件捕获数据 url 并将其用作发布 url
$('.search-input').each(function(){
$(this).on('change',function (e) {
search(this)
});
});
function search(self) {
var query = $(self).val();
$.ajax({
url: $(self).attr('data-url'),
type: 'post',
data: {'q':query, '_token': $('input[name=_token]').val()},
success: function(data){
console.log(data);
},
error: function(data){
// Not found
}
});
}
关于ajax - 如何在 Laravel js 文件上添加 ajax URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413665/