因此,在3个小时的大部分时间里,我一直在这里查看为什么我的代码无法正常工作。我不认为我的Ajax请求正在检测我的CSRF令牌,即使我尝试了多种方法来实现它也是如此。 AJAX请求的新手,轻松进行。
目标:
向/ email / subscribe /提交ajax POST请求,并将用户提供的电子邮件地址添加到email_subscribers表中。
我尝试将以下内容添加到代码中以显示令牌:
元标记和Ajax设置。
文件顶部
<meta name="csrf-token" content="{{ csrf_token() }}">
在脚本标签INSIDE中,jquery $(document).ready()
函数
// CSRF Ajax Token
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
隐藏的输入栏
我还添加了一个隐藏的输入字段,并尝试将令牌插入到ajax帖子内的数据对象中。
的HTML
<input type="hidden" id="token" value="{{ csrf_token() }}">
Javascript标签
var token = $('#token').val();
// var token = $('meta[name="csrf-token"]').attr('content'); //Tried this way
$.ajax({
type: 'POST',
url: '/email/subscribe/',
dataType: 'json',
data: {
email: email,
'_token': token,
// "_token": "{{ csrf_token() }}", //Tried this way as well
"_method": 'POST',
},
success: function (data) {
// Check Server Side validation
if($.isEmptyObject(data.errors)){
console.log(data['success']);
}else{
// Validation Failed Display Error Message
console.log(data.errors);
}
},
error: function (data) {
console.log(data);
}
}); // End Ajax POST function
这是我的web.php文件
// Email Routes
Route::prefix('email')->group(function() {
// Create Post Route for subscribing
Route::post('/subscribe', 'EmailSubscriptionsController@subscribe')->name('email.subscribe');
});
和我的EmailSubscriptionsController
class EmailSubscriptionsController extends Controller
{
// Store the Email into the database
public function subscribe(Request $request) {
// Validate the request
$validator = Validator::make($request->all(), [
'email' => 'required|email',
]);
// If the validation fails
if($validator->fails()) {
return response()->json([
'errors' => $validator->errors()->all(),
]);
}
// New Section Object
$subscription = new EmailSubscription;
// Add Name into Section Object
$subscription->email = $request->email;
// Save the Section
$subscription->save();
// Return The Request
return response()->json([
'success' => 'Record has been saved successfully!'
]);
} // End Subscribe
} // End Controller
真正奇怪的是,当我在控制器内的subscribe()
函数中使用NOTHING提交以下请求时:
// Return The Request
return response()->json([
'success' => 'Record has been saved successfully!'
]);
它不会返回错误。...只需将成功消息传递到控制台。我不确定自己在做什么错,因为我在网站的另一部分中有此工作(ajax发布请求)。
参考方案
首先在storage/logs/laravel.log
中查找异常堆栈跟踪。这应该可以更清楚地表明失败的原因。
Web检查器还允许您查看通常包含跟踪的响应。
500个ISE的常见原因是通过use
导入类不正确。
我目前正在学习Laravel。我想知道是否有一种方法可以在Laravel中使用Storage::从另一个硬盘访问文件(使用Windows)。例如,我在驱动器C:上安装了带有Laravel的Xampp,但是我想访问网站目录之外的E:上的文件。我试过使用Storage::files('E:')和File::files('E:…
检查对象是否已在集合中-Laravel - php当我循环一系列不同的结果时,我希望将对象添加到新集合中。查询:$osRed = Item::where('category', 'Hardware') ->where(function ($query) { $query->where('operating_system', '…
laravel Hash::make继续给出不同的结果 - php我想在laravel 4中使用身份验证我改了桌子和莫贝尔的名字当用户注册时,我拥有密码并将其保存,哈希为:$password = Hash::make(Input::get('password')); 然后当用户登录时,我想先对他/她进行身份验证。我这样做是这样的:if (Auth::attempt(array('usernam…
Laravel打印日志 - php我正在尝试在控制台上打印日志:我输入了:use Log; 然后在控制器中使用 Log::info('test log'); 但它不打印任何日志。 参考方案 我认为您需要在运行后检查storage/logs/laravel.logLog::info('test log'); 希望这对您有帮助
Laravel迁移外键 - php我正在尝试在Laravel中建立一些关系,我对关系和迁移有些困惑。这是我正在做的一个简单示例:Users -> has_many -> Cats 因此,我的用户迁移文件与Cats的关系如下:$table->foreign('cats_id')->references('id')->on(&…