laravel 4: URL::route vs jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17088313/
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 07:57:32  来源:igfitidea点击:

laravel 4: URL::route vs jquery

jquerylaravellaravel-4

提问by user2482384

(As new laravel user) I'm trying to build a ajax call url through the laravel URL::class:

(作为新的 Laravel 用户)我正在尝试通过 Laravel URL::class 构建一个 ajax 调用 url:

$.ajax( {
    url: '{{ URL::route('getUser', ['3']) }}', 
    success: function(results) {
    alert(results);
    }
});

routes.php:

路线.php:

Route::get('admin/getUser/{user_id}', array(
   'as' => 'getUser', 
   'uses' => 'AdminController@getUser'
));

Instead of the hard coded 3this parameter should come from jquery (e.g. $(this).attr('user_id')).

3这个参数应该来自jquery(例如$(this).attr('user_id'))而不是硬编码。

Can someone tell me how to create the URL dynamically?

有人能告诉我如何动态创建 URL 吗?

It seems that because of the route definition the URL::routefunction needs the parameter hardcoded or as a php varaible.

似乎由于路由定义,URL::route函数需要硬编码参数或作为 php 变量。

I hope this is +/- clear...

我希望这是 +/- 清楚...

Thanks for help anyway!

无论如何感谢您的帮助!

采纳答案by jeroentbt

You could keep the url clean and pass the variable as data in your ajax call.

您可以保持 url 干净并在 ajax 调用中将变量作为数据传递。

var urlGetUser = "{{ URL::route('getUser') }}",
    userId = $(this).attr('user_id');

$.ajax({
  type: "POST",
  url: urlGetUser,
  data: { id: userId }
}).done(function( msg ) {
  alert( msg );
});

This way you won't have to create ajax calls for every possible user ID. Something both other current solutions also offer.

这样您就不必为每个可能的用户 ID 创建 ajax 调用。其他当前的解决方案也提供了一些东西。

Disclaimer: Colleague of asker with absolutely no experience in Laravel :/

免责声明:提问者的同事完全没有 Laravel 经验:/

回答by Jason Spick

I've been using URL::to() for my Ajax calls. I've had problems to. But if you do use URL::to() you would be able to use javascript variables. try this:

我一直在使用 URL::to() 进行 Ajax 调用。我遇到了问题。但是,如果您确实使用 URL::to(),您将能够使用 javascript 变量。尝试这个:

$id = 3;
"{{URL::to('getUser/'".$id.")}}"

Hope this helps, I'm new too. if you find a better way to do this awesome, let me know ;)

希望这有帮助,我也是新手。如果您找到更好的方法来做到这一点,请告诉我;)

回答by Neo Ighodaro

Because PHP actually handles the file before passing it to the browser it may be indeed impossible to dynamically use a php variable directly, but theres a way around it. This may work ONLY if you havent type-casted your route parameter user_idto a certain definition.

因为 PHP 在将文件传递给浏览器之前实际上会对其进行处理,所以直接动态地使用 php 变量确实是不可能的,但是有一种方法可以解决它。仅当您没有将路由参数类型user_id转换为某个定义时,这才可能起作用。

// Add a placeholder which we'll use jQuery to swap out later
urlTo = "{{ URL::route('getUser', ['%userid%']) }}";

// swap out the placeholder dynamically using jQuery
urlTo = urlTo.replace('%userid%', $('#someElement').attr('user_id'));

$.ajax({
    url: urlTo, 
    success: function(results) {
        alert(results);
    }
});

What we've done here is to create a placeholder in our URL generated from a route so we'll probably have a URL like http://localhost/app/profile/%userid%after PHP has finished processing. We then use jQuery to replace the placeholder to our actual dynamic value.

我们在这里所做的是在从路由生成的 URL 中创建一个占位符,因此我们可能会http://localhost/app/profile/%userid%在 PHP 完成处理后得到一个 URL 。然后我们使用 jQuery 将占位符替换为我们的实际动态值。