如何在laravel js文件中添加ajax URL?

时间:2022-10-30 10:33:17

I am building a store locator for a website that I am building in Laravel. Since the blade file calls the js file tht is on the assests folder. It doesn't recognize the URL like this

我正在为我在Laravel建立的网站建立一个商店定位器。由于刀片文件调用js文件,因此它位于assests文件夹中。它不能识别这样的URL

$.ajax({
    url: '{{ URL::action('getLocation') }}',
    // ...
});

This is how I have my route.php

这就是我的route.php

Route::post('/getLocation', array('as'=>'getLocation','uses'=>'FrontController@getLocation'));

So it doesn't find the file. How can I call this function in the ajax URL?

所以它找不到文件。如何在ajax URL中调用此函数?

6 个解决方案

#1


3  

Here is a demonstration of how i would achieve this

这是一个如何实现这一目标的演示

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.

我可能会迟到。这只是一个示例代码,可帮助您了解访问此问题的人员。希望这可以帮助任何访问这里的人。

in my routes.php i define a named route

在我的routes.php中,我定义了一个命名路由

Route::post('getLocation',array(
    'as'=>'getLocation','uses'=>'FrontController@getLocation')
);

added name route as data-url in my somehtmlform.blade.php file

在somehtmlform.blade.php文件中添加了名称route作为data-url

{!! Form::open() !!}
{!! Form::text('input-name',null,array('class'=>'form-control search-input','data-url'=> URL::route("getLocation") ))
{!! Form::close() !!}

my search.js file catches the data-url and use it as post url

我的search.js文件捕获data-url并将其用作post 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
        }
    });
}

#2


2  

You can use this package, it gives almost all laravel helper functions which can be used in js files too.

你可以使用这个包,它提供几乎所有的laravel辅助函数,也可以在js文件中使用。

#3


1  

You may try this:

你可以试试这个:

// Add this in your filtes.php file (feel free to store where you like)
View::composer('layouts.master', function($view) {
    $ajaxUrl = json_encode(array('url' => URL::action('getLocation')));
    $view->with('ajax', $ajaxUrl);
});

Add this in you master.blade.php file's (master layout) <head></head> section (place it before your js file):

在你的master.blade.php文件(主布局) 部分中添加它(将它放在你的js文件之前):

<script>var ajax = {{ $ajax or 'undefined' }}</script>

Now you can use this as:

现在您可以将其用作:

// ajax.url
console.log(ajax.url);

Read here, similar thing.

读到这里,类似的事情。

#4


0  

It looks like you're using the wrong method for generating the URL. Try switching from URL::action() to URL::route().

看起来您使用了错误的方法来生成URL。尝试从URL :: action()切换到URL :: route()。

URL::action() is used to generate a URL to a given controller action, but then you need to write it like this:

URL :: action()用于生成给定控制器操作的URL,但是您需要像这样编写它:

URL::action('FrontController@getLocation')

URL::route() generates a url to a route which is named in the route definition, using "as" => "routeName". In your case:

URL :: route()使用“as”=>“routeName”为路由定义中指定的路由生成URL。在你的情况下:

URL::route('getLocation')

Hope this helps!

希望这可以帮助!

#5


0  

If you call your ajax function from a .js file,
try to change the blade part '{{ URL::action('getLocation') }}' to '/getLocation'
or pass a full url like: 'http://domain.com/getLocation' and it should work.

如果从.js文件中调用ajax函数,请尝试将刀片部分'{{URL :: action('getLocation')}}'更改为'/ getLocation'或传递完整的URL,如:'http:// domain.com/getLocation'它应该工作。

#6


0  

in js file, you can't not use the Url::action for route just do

在js文件中,你不能不使用Url :: action进行路由

url:"/getLocation"

#1


3  

Here is a demonstration of how i would achieve this

这是一个如何实现这一目标的演示

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.

我可能会迟到。这只是一个示例代码,可帮助您了解访问此问题的人员。希望这可以帮助任何访问这里的人。

in my routes.php i define a named route

在我的routes.php中,我定义了一个命名路由

Route::post('getLocation',array(
    'as'=>'getLocation','uses'=>'FrontController@getLocation')
);

added name route as data-url in my somehtmlform.blade.php file

在somehtmlform.blade.php文件中添加了名称route作为data-url

{!! Form::open() !!}
{!! Form::text('input-name',null,array('class'=>'form-control search-input','data-url'=> URL::route("getLocation") ))
{!! Form::close() !!}

my search.js file catches the data-url and use it as post url

我的search.js文件捕获data-url并将其用作post 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
        }
    });
}

#2


2  

You can use this package, it gives almost all laravel helper functions which can be used in js files too.

你可以使用这个包,它提供几乎所有的laravel辅助函数,也可以在js文件中使用。

#3


1  

You may try this:

你可以试试这个:

// Add this in your filtes.php file (feel free to store where you like)
View::composer('layouts.master', function($view) {
    $ajaxUrl = json_encode(array('url' => URL::action('getLocation')));
    $view->with('ajax', $ajaxUrl);
});

Add this in you master.blade.php file's (master layout) <head></head> section (place it before your js file):

在你的master.blade.php文件(主布局) 部分中添加它(将它放在你的js文件之前):

<script>var ajax = {{ $ajax or 'undefined' }}</script>

Now you can use this as:

现在您可以将其用作:

// ajax.url
console.log(ajax.url);

Read here, similar thing.

读到这里,类似的事情。

#4


0  

It looks like you're using the wrong method for generating the URL. Try switching from URL::action() to URL::route().

看起来您使用了错误的方法来生成URL。尝试从URL :: action()切换到URL :: route()。

URL::action() is used to generate a URL to a given controller action, but then you need to write it like this:

URL :: action()用于生成给定控制器操作的URL,但是您需要像这样编写它:

URL::action('FrontController@getLocation')

URL::route() generates a url to a route which is named in the route definition, using "as" => "routeName". In your case:

URL :: route()使用“as”=>“routeName”为路由定义中指定的路由生成URL。在你的情况下:

URL::route('getLocation')

Hope this helps!

希望这可以帮助!

#5


0  

If you call your ajax function from a .js file,
try to change the blade part '{{ URL::action('getLocation') }}' to '/getLocation'
or pass a full url like: 'http://domain.com/getLocation' and it should work.

如果从.js文件中调用ajax函数,请尝试将刀片部分'{{URL :: action('getLocation')}}'更改为'/ getLocation'或传递完整的URL,如:'http:// domain.com/getLocation'它应该工作。

#6


0  

in js file, you can't not use the Url::action for route just do

在js文件中,你不能不使用Url :: action进行路由

url:"/getLocation"