0

I have a simple page that is being served by AngulraJs application. The page has div with a value of Test.

<div id="test">Test</div>

My goal is to read this value from localhost. Basically, I am writing a plugin for an application. This application is a desktop app that has its own web server built in and plugins are written in javascript. My goal is to read the test value. The problem that I am facing though is that I get prevented by CORS.

From everything I have read (and I have been reading a lot), I need to add the Access-Control-Allow-Origin header in order for this to work. What my problem is how do I add a header with angular js for one page only.

I have found the follow SO answer: Set HTTP header for one request
I have also read a lot of documentation such as this: https://docs.angularjs.org/api/ng/service/$http

However, I am still confused with is one thing:

The url that is entered, what should it be in my case? Consider the following example, how would you write it to work for my case if the Angularjs app is sitting on www.somedomain.com and it is being called by javascript code from my localhost machine.

$http.get('what url goes here?', {
    headers: {'Access-Control-Allow-Origin': '*'}
});

I also have a small sub question, with the above example, will I be overriding any of the existing headers or will it only append a new header?

EDIT:

Angularjs code serving back the page:

(function() {
    'use strict';
    angular
        .module('App')
        .controller('Test', Test);

    function Test($scope, $http) { 
        var T = this;
        T.test = test;

        function test(){
            return "Test";
        }
    }
})();

HTML

<div id="test">{{T.test()}}</div>
Bagzli
  • 6,254
  • 17
  • 80
  • 163

1 Answers1

1

The header Access-Control-Allow-Origin must be set on returned header, not request header. So config your server to return the page 'what url goes here?' to have that header return.

If you have no control over 'what url goes here?' then you have to write a proxy with your web server: webserver get that page content then return to your script.

James
  • 13,571
  • 6
  • 61
  • 83
  • You have completely lost me in your explanation. the website that is making the request is coming from my own computer. The angularjs website that is returning the result is sitting on the server. I am adding that block of code on the conroller of the angularjs website. I just don't understand what URL is suppose to go there. The url of the angularjs website which is serving the response? – Bagzli Aug 18 '17 at 00:39
  • You call $http.get and pass the header to server is useless. You have to return that header from the return data from sever. Can you also post server code? – James Aug 18 '17 at 04:14
  • I updated the question. I understand that passing the header from the client to the server is useless. My whole question is how do I pass it from the server if my application is written in angularjs. – Bagzli Aug 18 '17 at 14:17
  • Angular is not server side. You may already use nodejs or something else to return that page. Search for any node express to see if there is any response header – James Aug 18 '17 at 16:01