We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to handle request from AngularJS?

Hi everyone,

I'm building a web API that will receive request from Ionic mobile app, which use AngularJS to make to request.

But the problem is, according to this

https://stackoverflow.com/questions/1256593/jquery-why-am-i-getting-an-options-request-instead-of-a-get-request

Because of "Preflighted requests", AngularJS first will make an OPTION request "to determine whether the actual request is safe to send"

How can I handle this kind of request? Because my API only support 1 kind of request for an URI (GET or POST or PUT...)

Thank you very much



8.1k
Accepted
answer

Angular don't make OPTIONS request on standard request. Only if you add additional headers to ajax request. If your API is on external server, you can set CORS headers from this server or from your application such as

response->setHeader('Access-Control-Allow-Headers',
        'Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Allow-Methods, Angular, Content-Type');

and other for allow ajax requests to external domain. I use this in any api servers CPhalcon and Angular building. Unfortunately, I can not show - they are for corporate use

Example request from site to API :

$http.get("/api....").then(function(response){...code});