1

I am building a hybrid App using ionic for the first time. I have to create a library which will connect all the controllers to the external API. I have authentication error. I have the following code:

angular.module('starter.services', [])

.factory('apiConnect',function(){
    var sslurl = 'urltoexternal Api';
    var apiConnect = function(){
       this.vesion = '3.0';
       this.prefix = 'oauth_';
       this.key = key;
       this.secret = secret;
       this.sslurl = '';        
    }
    apiConnect.prototype.callservice = function(call, $http,$resource) {        
    var endpoint = sslurl + call;        

    $http({ 
        method: 'POST',
        url: sslurl + call,
        headers: {
            'Access-Control-Allow-Origin' : '*',
            'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'oauth_nonce': '47901059',
        },
        data: {


        }
    }).then(function(resp) {
        console.log('Success', resp);

        // For JSON responses, resp.data contains the result
    }, function(err) {
        console.error('ERR', err);
        // err.status will contain the status code
    })
}

I get the following error

No 'Access-Control-Allow-Origin' header is present on the requested resource. I want to pass the following

  1. oauth_nonce,oauth_timestamp,oauth_key,oauth_signature in the header.
  2. I want to call POST method but it always call OPTIONS
  3. I want to send and receive json How do I pass this values and get the response? What is the correct approach for this kind of rest connection.
Web Developer
  • 133
  • 2
  • 14
  • 1
    The `access-control-allow-origin` header must be in the response from server not in the request (see several posts about CORS, for ex http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource, http://stackoverflow.com/questions/24097484/angular-js-no-access-control-allow-origin-header-is-present-on-the-requested-r, ...) – beaver Feb 25 '16 at 08:03

1 Answers1

0

access-control-allow-origin is browser related issue for ionic REST API consuming option.

You can relief from it using a chrome extension easily .

using CORS extension.

Please see the attached snap to enable it.Hope it will help you to enjoy ionic more to consume api.

Note:Sorry for very late answer because i have got it today

Thank you!