0

I have a statement in my code like this

  return response()->json([
     'error'=>$validator->errors()->all()
  ]);

When I view the response it has curly braces around it and looks like an object and it throws an error in the console that says

SyntaxError: Unexpected end of JSON input at parse ()

What do I need to change to get the format that my ajax call can interpret?

This code worked inside of a controller but when I move it to a different page it breaks so it seems there is something in the controller that corrects this, but is missing on my own page. Any ideas?

2 Answers 2

1

you can try something like below code

$data['success'] = false;
$data['message'] = $validator->errors()->all();
echo json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

that did it! with the exception that I had to add to it $validator->errors()->all();
1

obviously the json structure is wrong, u show use json_encode() to handle the $validator->errors()->all();

return response()->json([
 'error'=>json_encode($validator->errors()->all())

]);

1 Comment

thanks for this alternate idea. I clicked wrong button at first to downvote when I meant to upvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.