3

I know this has been asked time and time again but I cannot seem to fix my issue no matter what solution I try.

I'm trying to create a new user using AngularJS within Ionic. I have set up a small REST server on my localhost along with my testing of the app. So they are on the same server but are just in different folders. Nevertheless, I still need to apply CORS for some reason...

Using the $http method in AngularJS in a service I can do GET requests. However, when I try 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 method (I am using CodeIgniter and using Phil Sturgeon's REST server)

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
            );

        }

    }

}

The Error in the 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 solution or a nudge in the right direction would be a massive help. I've been working on this for 4 hours already and still cannot figure it out.

Brad Bird
  • 737
  • 1
  • 11
  • 30
  • 2
    The answers in the following link quick fixed my issue but I still don't know why it was happening in the first place... http://stackoverflow.com/questions/15602099/http-options-error-in-phil-sturgeons-codeigniter-restserver-and-backbone-js – Brad Bird Nov 20 '14 at 23:47
  • Hi! Add all request header into Access-Control-Allow-Headers option – GianArb Nov 20 '14 at 23:59

0 Answers0