-3

I'm using Laravel to make a request to an endpoint, which when I test locally works fine and returns what I'm expecting to the frontend, and even when I access the API route/Request URL in the browser it returns the proper response I see in the frontend (ex. http://127.0.0.1:8000/api/makeRequest?user_input=salmon%2Crice returns a perfectly fine response). However, when I open DevTools it tells me I'm getting a 500 Internal Server Error. What's wrong with my configuration?

api.php:

Route::post('/makeRequest', [App\Http\Controllers\ApiController::class, 'makeRequest'])->name('makeRequest.store');
Route::get('/makeRequest', [App\Http\Controllers\ApiController::class, 'makeRequest'])->name('makeRequest.store');

form in my view:

                <form id="recipeForm" method="post" action="/makeRequest">
                        @csrf
                        <input type="text" id="ingredientInput" placeholder="Ingredient" class="input input-bordered w-full max-w-xs">
                </form>

makeRequest function:

function makeRequest(ingredients) {
    if (ingredients.length > 0) {
        var userInput = ingredients;
        var url = `/api/makeRequest?user_input=${encodeURIComponent(
            userInput
        )}`;
        console.log(ingredients);

        fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                Accept: "application/json",
            },
            body: JSON.stringify({ ingredients: ingredients }),
        })
            .then((response) => response.text())
            .then((html) => {
                handleApiResponse(html);
            })
            .catch((error) => {
                console.error("Error:", error);
            });
    } else {
        alert("Please add at least one ingredient before submitting.");
    }
}

edit: adding a screenshot from Network tab in DevTools devtools

12
  • 4
    What do your server-side logs say about the 500 error?
    – Phil
    Apr 15 at 0:58
  • @Phil Hi, would these be the same ones found in ./storage/logs?
    – dejsdukes
    Apr 15 at 18:13
  • @dejsdukes yes.
    – francisco
    Apr 16 at 8:19
  • @francisco there's nothing about a 500 error when I check the logs
    – dejsdukes
    Apr 16 at 20:40
  • 2
    Please show dev tools network tab screenshot with selected failed request info and response tabs
    – Justinas
    Apr 17 at 6:14

0

Your Answer

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

Browse other questions tagged or ask your own question.