Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!