0

I'm creating an app using ionic and angularjs. Now I want to connect in a webservice but I don't know how can I do this. My webservice uses Basic Auth to connect and I am looking for some solution to create this connection to the webservice.

The webservice was created using CakePHP, note that if I use native language to Android or iOS I can connect, for example on Android I'm using Volley API to connect but using AngularJS I can't.

How could I use Basic Auth to connect on a webservice with AngularJS ?

I'm trying this.

Service

var app = angular.module("starter");

app.service("UserLoginAPI", function($http, AppConstants, Base64){
    this.doLogin = function(){
        var _url = AppConstants.webServiceUrl + "/users/testaWS.json";
        var _authdata = Base64.encode('user' + ':' + 'password');   

        return $http.post(_url, {
            headers: { 'Content-Type' : 'application/json; charset=UTF-8',
                       'Authorization' : 'Basic ' + _authdata
                    }
        });

    };
});

Exception

XMLHttpRequest cannot load http://192.168.1.105/AppPanel/users/testaWS.json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.

webservice CakePHP method

public function testaWS(){            
            $this->autoRender = false;                         
            return json_encode(array("message"=>"connected!"));
        }
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118

0 Answers0