简体   繁体   中英

GET 500 (Internal Server Error) laravel/js

I'm trying to send an OTP to a user when the user clicks the button.

so I'm using LARAVEL And I'm returning the response as JSON .

When I click the button I get this error in console :

GET http://domain/resend 500 (Internal Server Error)

SyntaxError: Unexpected token < in JSON at position 0

I read a question about the same topic the answer said that I have to add CSRF token, I did add it I still get the error.

my js code :

try {
        let resend = await fetch('http://domain/resend',{
            method:'GET',
            headers:{

                'Content-type':'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content').value
            },
        });
        let resultre = await resend.json();

        return {

            resultre
        }
    }catch (e) {
        console.log(e);
    }

And im returning the laravel reponse :

return response()->json($user->save());

I'm still beginner So my code might not be good,

thank you

This is server side related issue. Since your server outputs debug error code, probably in HTML, it is not valid JSON output and your Javascript code cannot parse it as JSON.

That means the error is probably related with the method being called via route where you have this portion of code:

return response()->json($user->save());

At first glance, it looks like you are trying to call save() method on null. Is your $user object missing? Try to get it from $request.

But still, you need to check your logs. You should set your application to debug mode, in .env file.

Then you should check the logs ( if monolog is not installed, install it ) Check more for Logging in Laravel here: https://laravel.com/docs/5.6/logging

Maybe it would be helpful to wrap your response in try catch block to see what error throws:

try{
    return response()->json($user->save());
}catch(\Exception $e){
    return response()->json(['error'=>$e->getMessage()]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM