laravel 4:URL :: route与jquery [英] laravel 4: URL::route vs jquery
问题描述
(作为新的laravel用户),我正在尝试通过laravel URL :: class:
(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.
免责声明:Asker的同事,在Laravel中完全没有经验:/
Disclaimer: Colleague of asker with absolutely no experience in Laravel :/
这篇关于laravel 4:URL :: route与jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!