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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22413665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:11:44  来源:igfitidea点击:

How do I add ajax URL on a laravel js file?

ajaxlaravellaravel-4

提问by Monica

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 中构建的网站构建商店定位器。由于刀片文件调用位于assests 文件夹中的js 文件。它无法识别这样的 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 中调用这个函数?

回答by aimme

Here is a demonstration of how i would achieve this

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

I might be late here. This is just a sample codeto help understand people who visit this question. Hope this helps anyone who visits here.

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

in my routes.phpi 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.phpfile

在我的somehtmlform.blade.php文件中添加了名称路由作为 data-url

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

my search.jsfile 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
        }
    });
}

回答by Chaudhry Waqas

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

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

回答by The Alpha

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.phpfile's (master layout) <head></head>section (place it before your jsfile):

将此添加到您master.blade.php文件的 ( master layout)<head></head>部分(将其放在您的js文件之前):

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

Now you can use this as:

现在您可以将其用作:

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

Read here, similar thing.

在这里阅读,类似的事情。

回答by Philip

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',它应该可以工作。

回答by Micael Gustafsson

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!

希望这可以帮助!

回答by yan

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

在 js 文件中,你不能不使用 Url::action 作为路由,只是做

url:"/getLocation"