Laravel Ajax呼叫控制器 [英] Laravel Ajax Call to Controller
问题描述
我对Laravel
很陌生,在访问Controller
中的特定方法时遇到问题.我正在尝试根据下拉菜单更改数据库值(语言).
I am pretty new with Laravel
and I am having an issue with accessing a specific method in my Controller
. I am trying to change a Database value (language) based on a dropdown menu.
settings.profile.blade
<select id="accountLanguage" class="form-control" method="GET">
<option value="en" {{ ($strLanguage == 'en') ? 'selected = "selected"' : "" }} >English</option>
<option value="es" {{ ($strLanguage == 'es') ? 'selected = "selected"' : "" }} >Español</option>
<option value="he" {{ ($strLanguage == 'fr') ? 'selected = "selected"' : "" }} >French</option>
</select>
ProfileController
public function index() {
$objUser = Auth::User();
$strLanguage = $objUser->lang;
return view("application.settings.profile",[
'objUser' => $objUser,
'strLanguage' => $strLanguage
]);
}
// THE METHOD I NEED ACCESS TO
function update( $strLang ) {
$objUser = Auth::User();
// UPDATE LANGUAGE
$bolUpdated = $objUser->updateLanguage( $strLang );
// RETURN
return response()->json( $bolUpdated );
}
路线
Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {
...
// PROFILE
Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] );
Route::get('test', 'ProfileController@update');
});
settings.profile.js
function initProfileManager() {
// GET ELEMENTS
var domUpdateLanguage = $('#accountLanguage');
var updateLanguage = function() {
// MAKE AJAX CALL
$.ajaxq( {
// url:'/settings/profile',
url:'./test',
method:'GET',
success: function( bolUpdated ) {
if( bolUpdated ) {
alert('OK');
}
},
fail: function() {
alert('NO');
}
});
location.reload();
};
domUpdateLanguage.on( 'change', updateLanguage );
当前方式,我收到此错误Too few arguments to function App\Http\Controllers\Settings\ProfileController::update(), 0 passed and exactly 1 expected
.我了解错误,但不确定如何传递参数.
The way it currently is, I get this error Too few arguments to function App\Http\Controllers\Settings\ProfileController::update(), 0 passed and exactly 1 expected
. I understand the error, but not sure how to pass the argument.
如果我取消对JS
中的url
行的注释,则永远不会进入update
方法,而最终只能运行两次index
.
If I uncomment the url
line from the JS
, I never get into the update
method and I just end up running index
twice.
任何帮助将不胜感激.
编辑1
奇怪.我尝试定义一个随机值,它仍然会给我这个错误.我认为您可能是对的,这是一个语法问题.虽然看不到为什么会发生.
Strange. I tried defining a random value and it would still give me that error. I think you might be right and it is a syntax issue. Can't see why it would happen though.
function initProfileManage(strLang) {
// GET ELEMENTS
var domUpdateLanguage = $('#accountLanguage');
var updateLanguage = function() {
// MAKE AJAX CALL
$.ajaxq({
// url:'/settings/profile',
url:'./test',
method:'POST',
data: {
strLang: newLang,
}
success: function( bolUpdated ) {
if( bolUpdated ) {
alert('OK');
}
},
fail: function() {
alert('NO');
}
});
location.reload();
};
// UPATE LANGUAGE EVENT
domUpdateLanguage.on( 'change', updateLanguage );
}
推荐答案
这是对您问题的完整答案
This is a complete answer to your question
使用POST请求而不是GET
由于您正在更新用户的语言,因此使用POST请求更为安全
Because you are updating the user's language it's more secure to use a POST request
// MAKE AJAX CALL
$.ajax( {
// url:'/settings/profile',
url:'./test',
method:'POST',
data: {
strLang: newLang
},
success: function( bolUpdated ) {
if( bolUpdated ) {
alert('OK');
}
},
fail: function() {
alert('NO');
}
});
请不要忘记通过data属性在您的帖子请求中传递strLang.
and don't forget to pass the strLang in your post request via the data attribute.
防止CSRF攻击
将csrf令牌存储在HTML元标记中:
Store the csrf token in a HTML meta tag:
<meta name="csrf-token" content="{{ csrf_token() }}">
自动将令牌添加到所有请求标头中:
Automatically add the token to all request headers:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
创建一条路由来处理您的ajax请求:
Route::post('test', 'ProfileController@update');
通过控制器中的$ request对象获取strLang:
function update( $request ) {
$objUser = Auth::User();
// UPDATE LANGUAGE
$bolUpdated = $objUser->updateLanguage( $request->strLang );
// RETURN
return response()->json( $bolUpdated );
}
如果您使用的是HTML5,则settings.profile.blade应该如下所示:
If you are using HTML5, your settings.profile.blade should look like this:
<select id="accountLanguage" class="form-control" method="GET">
<option value="en" {{ ($strLanguage == 'en') ? 'selected' : '' }} >English</option>
<option value="es" {{ ($strLanguage == 'es') ? 'selected' : '' }} >Español</option>
<option value="he" {{ ($strLanguage == 'fr') ? 'selected' : '' }} >French</option>
</select>
在您的索引方法中,$ objUser已经包含lang属性
In your index method, $objUser already contains the lang property
public function index() {
$objUser = Auth::User();
return view("application.settings.profile",[
'objUser' => $objUser
]);
}
从选择元素中获取新的语言:
Getting the new lang from select element:
$('#accountLanguage').change(function () {
initProfileManager(this.value);
});
这篇关于Laravel Ajax呼叫控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!