I'm working on a react app that uses an api created with Laravel. When i try to submit a form from the client I get POST http://localhost:8000/api/register 404 (Not Found). An interesting fact is that on postman works perfectly: postman When I checked Network tab on google chrome this is what I got: network network So the request somehow works.

This is how I send request on client

const submitHandle = async data => {
    if (!invalid) {
      props.history.push("/");
      props.modifyLoginForm({
        open: false,
        register: props.loginModal.register
      });
      console.log(data);
      try {
        const response = await axios.post(
          "http://localhost:8000/api/register",
          data
        );
        console.log("Returned data:", response);
      } catch (e) {
        console.log(`Axios request failed: ${e}`);
      }
    }
  };

Register method on backend.

  protected function create(array $data)
{

    if ($data['legitimation_id'][0] === 'E') {
        $user_type = 1;
    }
    else if ($data['legitimation_id'][0] === 'P') {
        $user_type = 2;
    }

    $data['user_type'] = $user_type;
    $data['password'] = Hash::make($data['password']);

    $user = User::create($data);

    return $user;
}

public function register(Request $request) {

    $data = $request->all();

    if ($this->validator($data)->fails()) {
        return $this->sendError('Validation Error.', $this->validator($data)->errors());
    }

    if (mb_substr($data['legitimation_id'], 0, 1) != 'P' && mb_substr($data['legitimation_id'], 0, 1) != 'E' ) {
        return $this->sendError('Validation Error.', array("legitimation_id" => 'Invalid legitimation ID!'));
    };

    $user = $this->create($data);

    $success = ['first_name' => $user['first_name'], 'last_name' => $user['last_name']];

    return $this->sendResponse($success, 'User register successfully.');
}

Any help would be appreciated.

0 Answers