0

i'm working on anghular 4 projet , and i have a problem about http request and response from the server like :

XMLHttpRequest cannot load http://192.168.1.110:8000/updateUser/Contacts/ebba56e9-72eb-971a-9bb6-552de74d0d69. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 405.

i have look for this prblem and i found that i have to add header attribut from the server side , i did it and i see that the response contain this header : HEADERS

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: GET, POST, OPTIONS,PUT
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Connection: close
Content-Type: application/json
Date: 2017 May 31 13:15:48
Host: 192.168.1.110:8000
X-Debug-Token: fbb39b
X-Debug-Token-Link: http://192.168.1.110:8000/_profiler/fbb39b
X-Powered-By: PHP/5.6.30

my controller is :

 /**
     *
     * @Get("/getMenu/{id}")
     */
    public function getMenuAction($id ,Request $request){
        $fr= $request->get('fr') ;
        $url = $this->container->getParameter('serverUri'). wsUrl::Ws_GetMenu . $uniquecsoecid. '?fr='.$fr;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, wsUrl::ws_Headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        $res = new Response();
        $res->setCharset('UTF-8');
        $res->headers->set('Access-Control-Allow-Method','PUT');
        $res->headers->set('Access-Control-Allow-Credentials','true');
        $res->headers->set('Access-Control-Allow-Origin','*');
        $res->headers->set('Access-Control-Allow-Headers','auth_token');
        $res->setContent($response);
        return $res ;
    }

for more details i'm using symfony to call another backend and return the result of this backend web services to my angular client side project .

but what is confused me is that the webservice work correctly if i try to send request from restlet or postman (chrome extension) and calling the web services from my angular is not working at all . what is the wrong , please some help

1 Answer 1

0

You are getting the error due to CORS. Postman is not restricted. However, setting the header in the WebService should be enough for you to have it working from your client application.

What browser are you using? Sometimes Chrome is a bit too sensitive with localhost and CORS. If so, can you try in a different browser? And here is the tool to work around it: Chrome CORS Extension

1
  • i getting 405 error , byt the way i'm using chrome browser May 31, 2017 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.