Can we see the form html too?
Mar 15, 2018
2
Level 4
Empty Ajax Request & Invalid argument supplied for foreach() error
The Issue
Request being sent via ajax is empty within the controller, results in Invalid argument supplied for foreach() error.
The majority of requests are submitted with no problems and have the data that is expected in the request. The error occurs seemingly randomly and we have been unable to reproduce the error even with replicating inputs of a user that receives in errors.
The issue starting occurring after updating Laravel Forge to PHP 7.1 from PHP 7.0. Once we noticed the error we downgraded to PHP 7.0 but the error persisted.
What we know
- There seems to be no relation between
- Browser
- Browser version
- User input
- Request is submitted via ajax
- There is no data in the request at the top of the controller
Relevant code where data is being sent via ajax
var signatureImageUrl = $("#jSignature").get(0).toDataURL();
$.ajax({
method: "POST",
url: "submit",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: {
"visit_data": $("form").serializeArray(),
"signature_img_url": signatureImageUrl,
}
});
Relevant code where the error is being thrown within the controller
public function submit(Request $request) {
try {
foreach ($request->input('visit_data') as $index => $visitComponent) {
// Iterates over data
}
} catch (\ErrorException $e) {
// Handles error
}
Please or to participate in this conversation.