Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andersonmichel's avatar

Status 500, Internal Server Error in api using fetch

I have a function in a Vue instance, and I'm trying to update my data by api. Using Postman I can test and it works pretty fine. But using Javascript it dont works, and I can see in console a status 500 "Internal Server Error". Can someone help my with my code?

save(){
                const url_save = '{{ url('/api/menus') }}';
                var metodo = 'POST';
                if(this.menu.id != ''){
                    metodo = 'PUT';
                }
                var post = {
                    id: this.menu.id,
                    nome: this.menu.nome,
                    itens: this.menu.itens,
                    localizacoes: this.menu.localizacoes,
                }; alert(JSON.stringify(post));
                var options = {
                    method: metodo,
                    body: JSON.stringify(post),
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    }
                };
                console.log(options)
                fetch(url_save, options).then(res => {console.log(res)
                    return res.json();
                }).then(json => {
                    alert("O menu foi salvo com sucesso!");
                }).catch(err => console.error('Caught error: ', err));
}
0 likes
3 replies
ftiersch's avatar

Multiple points here:

  1. Where is that javascript code? Is it in a blade file? Otherwise that won't work:
'{{ url('/api/menus') }}'
  1. What does your logfile say what the actual error was? :)
munazzil's avatar

I think you have some type error. var metodo = 'POST'; Change it to method as like below.

var method= 'POST';

and

method= 'PUT';

    var options = {
                method: post,

Please or to participate in this conversation.