I have this AJAX function, which sends data to a route:
$.ajax({
url : '/home/update_user_data',
type : "post",
contentType: 'application/json;charset=UTF-8',
data : {'userid' : $('#user-general-i').data('userid'), 'datatype' : datatype, 'newcontent' : newcontent},
success : function(response) {
console.log("update_user_data", response);
if (response['success'] == true) {
console.log('success');
} else {
}
},
error : function(xhr) {
console.log("update_user_data", xhr);
}
});
In the route I cant figure out how to access the data, usually I can use $data->value but here it does not work. I assume because I dont serialize a form.
Here is how I try to access the data:
public function update_user_data(Request $data) {
try {
error_log($data);
error_log(print_r($data->all(), True));
error_log($data->query('userid'));
error_log($data->userid);
error_log($data->datatype);
error_log($data->newcontent);
} catch (\Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
return ['success' => true, 'message' => 'Konnte Nutzer nicht updaten'];
}
I have red the laravel docu on requests and tried all ways to access the data, but I simply get an empty string, no errors.
BUT error_log($data); shows the request and there I can see that the data is there:
userid=4777&datatype=gender&newcontent=1
EDIT
I can get the above string with:
error_log($data->getContent());
But I cant get specific values, I could use regex to extract them or split the string, but that feels super hacky and wrong.
EDIT
I also tried to change the AJAX request:
data : JSON.parse(JSON.stringify(({'userid' : $('#user-general-i').data('userid'), 'datatype' : datatype, 'newcontent' : newcontent})))
I also tried to add:
dataType: "json"
But sadly nothing changed, all error_logare empty.
Laravel Version: Laravel Framework 5.8.27