我正在try 使用PUT请求更新数据库中的值,但我遇到了CSRF错误,并且我不知道如何在Flutter 中修复它,所以我使用了Route::withoutMiddleware(['csrf'])
;
我期待记录被更新,但我仍然有错误:
PUT http://localhost/update/mike@fmaila.com 419(未知状态)
我还try 在VerifyCsrfToken.php文件中添加http://localhost/*
我怎么才能修好它呢?
就本报告而言:
public function updateStatus(Request $request, $email)
{
$validatedData = $request->validate([
'is_active' => 'required|string',
]);
$record = AddSeller::where('email', $email)->firstOrFail();
$record->is_active = $validatedData['is_active'];
$record->save();
return response()->json(['message' => 'Record updated successfully'], 200);
}
Flutter
Future<void> updateState(String email, String newValue) async {
final apiUrl = 'http://localhost/update/$email';
final headers = {'Content-Type': 'application/json'};
final requestData = {'is_active': newValue};
try {
final response = await http.put(
Uri.parse(apiUrl),
headers: headers,
body: jsonEncode(requestData),
);
if (response.statusCode == 200) {
print('Record updated successfully');
} else {
print('Failed to update record: ${response.body}');
}
} catch (e) {
print('Error: $e');
}
}