Код: Выделить всё
//// temp.blade.php file
$(document).ready(function() {
$('#productForm').on('submit', function(e) {
let data = $('#productForm').serialize();
$.ajax({
url: "posts",
type: "POST",
datatype: "json",
data: {
"_token": "{{ csrf_token() }}",
data: data
},
success: function(response) //call back function
{
$('#respanel').html(response);
$('#productForm')[0].reset();
alert(response.message);
},
error: function(error) {
console.log(error);
alert(error.message);
}
});
});
});
< /code>
Файл маршрута < /p>
//// web.php file
Route::get('posts', [PostController::class, 'index']);
Route::post('posts', [PostController::class, 'store']);
< /code>
controler class :: < /p>
class PostController extends Controller
{
public function store(Request $request)
{
try {
return response()->json([
'status'=>200,
'message'=> $request->data
]);
} catch (Exception $e) {
return response()->json([
'status'=>200,
'message'=>$e->getMessage()
]);
}
}
}
< /code>
Когда я отправляю данные как name = 'test' и detail = 'dummy-details' из моего файла лезвия, метод хранилища контроллера Показать ответ как < /p>
id=&name=test&detail=dummy-details
< /code>
Но когда я изменяю переменную сообщения ответа возврата на < /p>
return response()->json([
'status'=>200,
'message'=> $request->name // or $request->input('name')
]);
и когда я делаю
Код: Выделить всё
return response()->json([
'status'=>200,
'message'=> $request->all()
]);
В чем проблема с форматом данных? Как получить данные из переменной запроса?
Спасибо !!! < /p>
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-variable