OPTIONS Error when using Ionic AngularJS $ http PUT, POST or DELETE requests

I know this has been asked over and over again, but I cannot fix my problem no matter which solution I try.

I am trying to create a new user using AngularJS in Ionic. I set up a small REST server on my localhost along with my application testing. Thus, they are on the same server, but in different folders. However, I still need to enforce CORS for some reason ...

Using the $ http method in AngularJS on the service, I can make GET requests. However, when I try to make a PUT, POST, or DELETE request, I get an OPTIONS error in the console. Below are the settings I have ...

API index.php File

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

      

My UsersSvc

app.factory('UsersSvc', function($http, $location){

    return {

        /**
         * Adds a user to the database
         *
         * @param user
         * @returns {*}
         */
        addUser: function(user){

            // User Format...
            // user.user_email, user.user_password

            return $http.post($location.apiUrl + '/credentials/add_user',
                {user: user}).

                success(function(data){return data});

        }

    };

});

      

Rest Server API (I am using CodeIgniter and using REST Server by Phil Sturgeon)

class Credentials extends REST_Controller {

    public function add_user_post(){

        $user = $this->user_model->add_user($this->post('user'));

        if($user){

            $this->response($user, 200);

        }
        else{

            $this->response(
                ENVIRONMENT == 'development' ? $this->db->last_query(): null,
                ENVIRONMENT == 'development' ? 404 : 204
            );

        }

    }

}

      

Error in Chrome console

OPTIONS http://localhost/z-projects/git-motostatsfree/MotoStats/MotoStatsAPI/credentials/add_user 
(index):1 XMLHttpRequest cannot load http://localhost/z-projects/git-motostatsfree/MotoStats/MotoStatsAPI/credentials/add_user. Invalid HTTP status code 404

      

Request headers

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,en-GB;q=0.6
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Origin:http://localhost:8100
Pragma:no-cache
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36

      

Response headers

Access-Control-Allow-Headers:Origin, X-Requested-With, content-type, accept
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:42
Content-Type:application/json; charset=utf-8
Date:Thu, 20 Nov 2014 23:04:35 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Unix) PHP/5.5.9 OpenSSL/1.0.1f mod_perl/2.0.8-dev Perl/v5.16.3
Set-Cookie:ci_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22326db0454f2f256da6b6a526ed62cd2d%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A3%3A%22%3A%3A1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A120%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_10_1%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F39.0.2171.65+Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1416524675%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D3a3dfab89040b0df83c80eef434d063f; expires=Fri, 21-Nov-2014 01:04:35 GMT; Max-Age=7200; path=/
X-Powered-By:PHP/5.5.9

      

Any decision or nudge in the right direction would be a huge help. I've been working on this for 4 hours now and still can't figure it out.

+3


source to share





All Articles