enabling cors in codeigniter ( restserver by @chriskacerguis )

后端 未结 6 722
鱼传尺愫
鱼传尺愫 2020-12-03 12:38

http.get request in agularJs controller works fine when my client app and api are in localhost. when api is moved to server., issue arised.

client side using angular

6条回答
  •  温柔的废话
    2020-12-03 13:01

    Try adding OPTIONS to the allowed methods.

    header("Access-Control-Allow-Methods: GET, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
    

    and return immediately when the request is method 'OPTIONS' once you have set the headers.

    if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) {
        die();
    }
    

    See also this answer.

    Angular sends a W3C CORS spec compliant preflight request that will check for the right allowed methods before actually attempting it.

    Personally, I find the Mozilla Developer Network CORS page a bit easier to read on the matter to help understand the flow of CORS.

提交回复
热议问题