I’m trying to send my data through XMLHttpRequest but don’t know how to format the string with all the data
My JS Code:
function savePhotos() {
var csrf = document.querySelector('meta[name="_token"]').content;
var xhr = new XMLHttpRequest();
xhr.open("POST", saveRoute, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.setRequestHeader("X-CSRF-TOKEN", csrf);
xhr.send("name='test'"); //What do I put here
}
My Controller create function:
public function create(Request $request)
{
$data = $request->all();
$model = Photo::create([
'categoryId' => $data['categoryId'],
'collectionId' => $data['collectionId'],
'categoryId' => $data['categoryId'],
'name' => $data['name'],
'views' => $data['views'],
'likes' => $data['likes'],
'dateUploaded' => $data['dateUploaded'],
'altText' => $data['altText'],
'url' => $data['url'],
'private' => $data['private'],
]);
return redirect()->route("photoIndex");
}