0

I am making simple todo app, with Symfony 4 in the back-end and Vue.js for front-end.

I am getting a 405 error status for unknown reasons since a few other endpoints are working correctly.

Here is part of my listController:

/**
 * @return \FOS\RestBundle\View\View
 */
public function getListsAction()
{
    $data = $this->taskListRepository->findAll();
    return $this->view($data, Response::HTTP_OK);
}


/**
 * @Rest\RequestParam(name="title", description="Title of the list", nullable=false)
 * @param ParamFetcher $paramFetcher
 * @return \FOS\RestBundle\View\View
 */
public function postListsAction(ParamFetcher $paramFetcher)
{
    $title = $paramFetcher->get('title');
    if ($title) {
        $list = new TaskList();
        $preferences = new Preference();
        $preferences->setList($list);
        $list->setPreferences($preferences);
        $list->setTitle($title);
        $this->entityManager->persist($list);
        $this->entityManager->flush();
        return $this->view($list, Response::HTTP_CREATED);
    }
    return $this->view(['title' => 'This cannot be null'], Response::HTTP_BAD_REQUEST);
}

the get action is working correctly here it allows me to get all lists from db

and nelmio_cors.yaml file:

nelmio_cors:
    defaults:
        origin_regex: true
        allow_credentials: true
        allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
        allow_methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
        allow_headers: ['Content-Type', 'Authorization', 'origin', 'Accept', 'bearer', 'Allow']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/': ~

as you can see POST method is allowed.

debug:router log : enter image description here

as shown the method occurs under expected method and endpoint, register and login_check, where method is not specified working correctly with using POST

Network error:

enter image description here

and in console I am getting CORS error.

yivi
  • 42,438
  • 18
  • 116
  • 138
Jake11
  • 801
  • 2
  • 11
  • 24
  • 1
    You need to ensure the server system allows OPTIONS requests. That’s a separate issue from your CORS config. If you’re running your php/symphony4 setup under Apache, then it may be that OPTIONS requests are being blocked by Apache; if so, see the answer at https://stackoverflow.com/a/45490500/441757 for guidance on how to make Apache allow OPTIONS requests. – sideshowbarker Nov 25 '19 at 04:04
  • Jake, seen the answer below? Any feedback? – yivi Nov 25 '19 at 12:26
  • yivi I will feedback, later i am in job right now – Jake11 Nov 25 '19 at 12:28

2 Answers2

1

You are making an OPTIONS request (that's a preflight request for CORS):

request headers

But our server is telling you clearly that it only accepts GET or POST requests:

response headers

You need to change your webserver configuration (Apache, Nginx, whatever you are using) so it also accept OPTIONS requests.

Pre-flight requests are issued automatically by the browser on certain circumstances, which explains why some of your endpoints were working, and not this one.

It looks like you are using the built-in PHP web-server, which is rather limited in features. I'm not aware of any way of changing its configuration. You can try serving accessing the site on 127.0.0.1 rather than localhost, which sometimes makes the browser being laxer on CORS requests.

yivi
  • 42,438
  • 18
  • 116
  • 138
-1

You can try this :

  1. Make sure that apache header module is activate by : a2enmod headers
  2. And to allow the webserver to accept OPTIONS (since it is needed for check CORS), add it to your .htaccess for example like this : Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Mahefa
  • 418
  • 3
  • 12