选项使用Ionic AngularJS $ http PUT,POST或DELETE请求时出错

时间:2014-11-20 23:17:25

标签: angularjs rest http-headers cors ionic-framework

我知道这已经被一次又一次地问过,但无论我尝试什么解决方案,我似乎无法解决我的问题。

我试图在Ionic中使用AngularJS创建新用户。我在我的localhost上设置了一个小型REST服务器以及我对该应用程序的测试。所以他们在同一台服务器上,但只是在不同的文件夹中。不过,我仍然需要出于某种原因申请CORS ......

在服务中使用AngularJS中的$ http方法我可以执行GET请求。但是,当我尝试PUT,POST或DELETE请求时,我在控制台中收到OPTIONS错误。以下是我的设置...

API index.php文件

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

我的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方法(我正在使用CodeIgniter并使用Phil Sturgeon的REST服务器)

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

        }

    }

}

Chrome控制台中的错误

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

请求标题

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

响应标题

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

任何正确方向的解决方案或推动都将是一个巨大的帮助。我已经做了4个小时的工作,但仍然无法理解。

0 个答案:

没有答案