[英]Laravel Ajax Call to Controller
我對Laravel
,在Controller
訪問特定方法時遇到問題。 我正在嘗試根據下拉菜單更改數據庫值(語言)。
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
。 我了解錯誤,但不確定如何傳遞參數。
如果我取消注釋JS
的url
行,則永遠不會進入update
方法,而最終運行兩次index
。
任何幫助,將不勝感激。
編輯1
奇怪。 我嘗試定義一個隨機值,它仍然會給我這個錯誤。 我認為您可能是對的,這是一個語法問題。 雖然看不到為什么會發生。
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 );
}
這是對您問題的完整答案
使用POST請求而不是GET
由於您正在更新用戶的語言,因此使用POST請求更加安全
// 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。
防御CSRF攻擊
將csrf令牌存儲在HTML元標記中:
<meta name="csrf-token" content="{{ csrf_token() }}">
自動將令牌添加到所有請求標頭:
$.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應該如下所示:
<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屬性
public function index() {
$objUser = Auth::User();
return view("application.settings.profile",[
'objUser' => $objUser
]);
}
從select元素獲取新的lang:
$('#accountLanguage').change(function () {
initProfileManager(this.value);
});
將路線更改為:
Route::post('test', 'ProfileController@update');
和你的ajax對象:
method:'POST',
實際上,您也必須以某種方式傳遞$ strLang(因為update()方法期望這樣做,因為錯誤消息指出)。
哦,您真的不需要選擇的HTML中的method屬性;)
您應該在路線中定義語言
Route::get('test/{strLang}', 'ProfileController@update');`
並將其作為變量傳遞給js代碼中的url: '/test/' + strLang
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.