laravel 4: URL::route vs jquery [英] laravel 4: URL::route vs jquery
问题描述
(作为新的 laravel 用户)我正在尝试通过 laravel URL::class: 构建一个 ajax 调用 url:
(As new laravel user) I'm trying to build a ajax call url through the laravel URL::class:
$.ajax( {
url: '{{ URL::route('getUser', ['3']) }}',
success: function(results) {
alert(results);
}
});
routes.php:
Route::get('admin/getUser/{user_id}', array(
'as' => 'getUser',
'uses' => 'AdminController@getUser'
));
这个参数应该来自 jquery(例如 $(this).attr('user_id')
),而不是硬编码的 3
.
Instead of the hard coded 3
this parameter should come from jquery (e.g. $(this).attr('user_id')
).
谁能告诉我如何动态创建 URL?
Can someone tell me how to create the URL dynamically?
似乎由于路由定义,URL::route
函数需要硬编码参数或作为 php 变量.
It seems that because of the route definition the URL::route
function needs the parameter hardcoded or as a php varaible.
我希望这是 +/- 清楚...
I hope this is +/- clear...
还是谢谢你的帮助!
推荐答案
您可以保持 url 干净并在 ajax 调用中将变量作为数据传递.
You could keep the url clean and pass the variable as data in your ajax call.
var urlGetUser = "{{ URL::route('getUser') }}",
userId = $(this).attr('user_id');
$.ajax({
type: "POST",
url: urlGetUser,
data: { id: userId }
}).done(function( msg ) {
alert( msg );
});
这样您就不必为每个可能的用户 ID 创建 ajax 调用.其他当前的解决方案也提供了一些东西.
This way you won't have to create ajax calls for every possible user ID. Something both other current solutions also offer.
免责声明:完全没有 Laravel 经验的提问者的同事:/
Disclaimer: Colleague of asker with absolutely no experience in Laravel :/
这篇关于laravel 4: URL::route vs jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!