ajax - 如何在 Laravel js 文件上添加 ajax URL?

标签 ajax laravel laravel-4

我正在为我在 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/

相关文章:

javascript - Laravel 和变量命名约定(蛇形 + 驼形)

拉拉维尔 4 : Joining two tables using Eloquent for multiple database connections

php - 在 Composer laravel 上安装依赖项(doctrine/dbal)

php - 单击 HTML img 标签并使用 PDO 通过 jQuery/AJAX 更新 MySQL

javascript - 无法通过 AJAX 调用找到 $wpdb 变量

ajax - ajax 页面的永久链接最佳实践

javascript - 我想在数据发生变化时更改 setTimeout 值

Laravel 5 CORS - XMLHttpRequest 无法加载 http ://myapi. com.预检响应具有无效的 HTTP 状态代码 500

Laravel mongodb 事务不回滚

php - WHERE 子句日期大于 24 小时 - Carbon/Laravel