Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
next.js fetch data from Laravel

i am writing a simple user register using Laravel with a simple code

public function register()
{
    $this->validate(request(), [
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required'
    ]);

    $user = User::create(request(['name', 'email', 'password']));
    
    auth()->login($user);

    return [
        'status' => true,
        'user' => $user
    ];
}

then i am sending the data form Next.Js :

using axios:

let config = {
    method: 'post',
    url: `http://localhost:8000/api/register`,
    headers: {
        'Content-Type': 'application/json',
    },
    data: values,
}
axios(config)
.then(json => console.log(json))

it's work fine, but i send an used email address, it will through 422 code, and axios can't catch the result

using fetch:

fetch('http://localhost:8000/api/register', {
    method: "post",
    mode: 'no-cors',
    body: new URLSearchParams(data)
}).then(res => res.json())
.then(json => console.log(json))

also work but when use used email, it will send 302 code , and call the index /

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think that you have missed adding the .catch(...) to return the error object like the example in docs Axios docs

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda