0

Facing CORS in angular, when i was trying to make a API call between my localhost to another domain.I am getting 404 issue .

1.Front End : Angualr 7

Front end request part:

const httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': 'true',
        'Access-Control-Allow-Methods':'POST',
        'Access-Control-Allow-Headers': 'Content-Type'
    })
  }
login(username: string, password: string) {
        return this.http.post<any>('http://remote/djaxtesting/enter_uiupgrade/index.php/api/v1/user/validate',
        {acc_type: "ADMIN", uemail: "djax_admin@dreamajax.com", upw: "123456"},httpOptions)
            .pipe(map(user => {}))


    }

Back end coding :

<?php defined('BASEPATH') OR exit('No direct script access allowed');
header ("Access-Control-Allow-Origin: *");
header ("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');

    public function validate_post()
    {

        $role = array('ADVERTISER','TRAFFICKER','ADMIN','MANAGER');

        if($this->post('acc_type') !='' and in_array($this->post('acc_type'),$role))
        {

            switch(strtoupper($this->post('acc_type')))
            {
                case "ADMIN":

                    $adminObj   =   $this->do_networks->validate_user($this->post('uemail'),$this->post('upw'),$this->post('acc_type'));
                    //$this->response($adminObj, 200);
    }
    }
    }

enter image description here

We using php for api. Helping handing needs to solve this issue ?

Shubham Azad
  • 677
  • 8
  • 22
Test Test
  • 1
  • 1

1 Answers1

0

The problem with the option method. Option request should be a 200 returning an empty response. Then the browser will send the real POST request.

for that replace with the headers in your PHP File in the constructor. It will work.

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
   $method = $_SERVER['REQUEST_METHOD'];
   if ($method == "OPTIONS") {
       die();
   }
Shubham Azad
  • 677
  • 8
  • 22
  • Yes i am getting request method is "OPTIONS".But i request post call only in angular (Front End side) – Test Test Feb 11 '19 at 10:43
  • Yes i tried your code set. But i am not get inside the "Validate Function".Because my "Validate" function is post method My PHP code: public function validate_post() { $this->response('test',200); } } } – Test Test Feb 11 '19 at 10:53
  • remove _post in your validate function and echo anything. and let me know the response. – Shubham Azad Feb 11 '19 at 10:57
  • for why the option method is set. check this SO answer.https://stackoverflow.com/questions/1256593/why-am-i-getting-an-options-request-instead-of-a-get-request/13030629#13030629 – Shubham Azad Feb 11 '19 at 11:02
  • I am using rest api in code igniter for that we must use the "POST or GET" methods .If i removed same error occured – Test Test Feb 11 '19 at 11:09
  • this is very simple in CodeIgniter.add header code in the constructor.it will work. – Shubham Azad Feb 11 '19 at 11:15
  • function __construct(){ parent::__construct(); header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization"); header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE"); } – Test Test Feb 11 '19 at 12:35
  • I added like above in constructor and tried also .But getting same error still. – Test Test Feb 11 '19 at 12:35
  • add this after the header in constructor $method = $_SERVER['REQUEST_METHOD']; if ($method == "OPTIONS") { die(); } – Shubham Azad Feb 12 '19 at 04:53