【发布时间】:2019-07-25 14:49:42
【问题描述】:
我在将数据从 javascript 发送到我的控制器时遇到问题。 还有我的Ajax:
var point = JSON.stringify(points);
function onBtnClick() {
$.ajaxSetup({
header:{
'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
}
});
$.post('http://localhost/updateC', {
data: point,
dataType: 'json',
contentType:'application/json',
})
.done(function() {
alert('success');
})
.fail(function() {
alert("error");
});
}
路线:
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('/races','RacesController');
Route::post('updateC', 'RacesController@Points');
还有我的 RacesController:
public function Points(Request $request) {
$test = $request->input('data');
return "$test";
}
错误是说它已被 CORS-polices 阻止。
【问题讨论】:
-
需要添加 x-csrf-token 标头,查看文档:laravel.com/docs/5.7/csrf
-
您必须发布哪些数据?...或显示您的表格?
-
数据是我创建的点 points[currentid] = {id: currentid, x: event.offsetX, y: event.offsetY}; currentid++;
-
您是否在您的ajax 发送页面中添加了
<meta name="csrf-token" content="{{ csrf_token() }}">。并请您的浏览器控制台截图。 Ajax 请求。 -
我试过了还是不行。但是这是我的错误link
标签: javascript ajax laravel