Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Vikram FusionAppliedApr 2 2016 — edited Jun 21 2017

I have a simple Oracle JET app thats retrieving files in a folder from Oracle document cloud service. I used Andrejus Baranovskis' example as a sample app

require(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojknockout', 'ojs/ojmodel',

    'ojs/ojtable', 'ojs/ojpagingcontrol'], // add additional JET components to the end of this list as needed 

        function (oj, ko, $) // this callback gets executed when all required modules are loaded 

        {

            function ViewModel() {

                var self = this;

                self.username = 'username';

                self.password = 'password';

                var creds = self.username + ':' + self.password;

                var basicScheme = btoa(creds);

                var hashStr = "Basic "+basicScheme

                function setAuthHeader(xhr){

                    var creds = self.username + ':' + self.password;

                    var basicScheme = btoa(creds);

                    var hashStr = "Basic "+basicScheme;

                    xhr.setRequestHeader('Authorization', hashStr);

                    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');

                }

               

                function make_base_auth(user, password) {

                    var tok = user + ':' + password;

                    var hash = btoa(tok);

                    return 'Basic ' + hash;

                }

                self.myOAuth = new oj.OAuth('X-Authorization', {'Authorization':hashStr, 'Access-Control-Allow-Origin':'*'});

                self.serviceURL = 'https://server.us2.oraclecloud.com/documents/api/1.1/folders/folderid/items';

                //self.Employees = ko.observableArray([]);

                self.DCSCol = ko.observable();

                self.pagingDatasource = ko.observable();

                self.fetch = function () {

                    self.DCSCol().fetch({

                        success: function(jqXHR, textStatus, errorThrown){

                            alert('Success !!'+textStatus);

                        },

                        error: function (jqXHR, textStatus, errorThrown) {

                            alert('Error in fetch: ' + textStatus);

                            console.log('Error in fetch: ' + textStatus);

                        }

                    });

                }

                parseDoc = function (response) {

                    return {type: response['type'],

                        name: response['name'],

                        id: response['id'],

                        version: response['version']};

                };

                var DCSDocument = oj.Model.extend({

                    parse: parseDoc,

                    idAttribute: 'id'

                });

                var myDoc = new DCSDocument();

                var DCSCollection = oj.Collection.extend({

                    url: self.serviceURL,

                    //beforeSend: function (xhr) {

                    //    xhr.setRequestHeader('Authorization', make_base_auth('weblogic', 'welcome1'));

                    //    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');

                    //},

                    fetchSize:10,

                    model: myDoc

                });

                self.DCSCol(new DCSCollection());

                self.pagingDatasource = new oj.PagingTableDataSource(new oj.CollectionTableDataSource(self.DCSCol()));

            }

            // Create a view model and apply it to the document body. This causes any 

            // ojComponents specified in the HTML data-bind to be initialized and their 

            // attributes evaluated using the view model. 

            ko.applyBindings(new ViewModel(), document.body);

        }

Here is the response on the server when I run the app on NetBeans:

Failed to load resource: the server responded with a status of 401 (Authorization Required) (15:17:39:088 | error, network)

  at https://<server>.oraclecloud.com/documents/api/1.1/folders/<folderid>/items?limit=10&offset=0&totalResults=true

XMLHttpRequest cannot load https://<server>.oraclecloud.com/documents/api/1.1/folders/<folderid>/items?limit=10&offset=0&totalResults=true. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. The response had HTTP status code 401. (15:17:39:090 | error, javascript)

  at index.html

Any idea what I need to change to fix this issue?

thnks,

Vikram

Comments
Post Details
Added on Apr 2 2016
14 comments
10,821 views