tech4
tech4

Reputation: 567

AngularJS Send a GET Request With Basic Authentication

I try to send a GET RESTful request to a server with basic authentication. I wrote the following code to achive this task:

module.controller("MainCtrl", ['$scope', '$http', function($scope, $http) {
            $http.get('http://192.168.10.15:8080/web/services/api/records/20420?_type=json', {
                headers:  {
                    'Authorization' : 'Basic username:password'}
            })
            .success(function(response) {
                console.log("my success");
                $scope.values = response;
              })

            }]
        );

When the request is processed, the given error is displayed on the browser console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource on http://192.168.10.15:8080/web/services/api/records/20420?_type=json. (Reason: header CORS 'Access-Control-Allow-Origin' missing).

Any Idea?

Upvotes: 1

Views: 392

Answers (3)

Marin
Marin

Reputation: 1020

Read this page http://amodernstory.com/2014/12/27/using-cors-headers-with-java-example/ and you should pay attention to this paragraph "The main idea to remember when writing your own filter is that, before any AJAX calls are made. The browser sends an OPTIONS AJAX request to the server. The server needs to reply to this OPTIONS call with the required permissions. " :D

Upvotes: 2

Markus
Markus

Reputation: 1563

if you're using node.js & express you can allow CORS like this:

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
    next();
});

in the express config file.

Upvotes: 0

t0mpl
t0mpl

Reputation: 5025

Your server (http://192.168.10.15:8080/) doesn't allow you to send request from external (Or just your server origin server).

You have to allow CORS (Cross-origin resource sharing) on your server, there are several ways to do it, in your server config.

Example

Upvotes: 0

Related Questions