Getting Data In

Using AngularJS to connect to Splunk's REST API, how do I resolve this error while getting the session key?

Engager

Hi Team,

I am using AngularJS to connect to Splunk's REST API, but I am getting

"XMLHttpRequest cannot load https://localhost:8089/services/auth/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access." 

in Chrome's console.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <script src="angular.js"></script>

    </head>


    <body>

    <div ng-app="myApp" ng-controller="myController">

    <p>{{  sessionKey  }}</p>
    </div>

    </body>


    <script>
    var app = angular.module('myApp', []);
    var req = {
                method: 'POST',
                url: 'https://localhost:8089/services/auth/login',
                headers : {
                    'Content-Type':'application/x-www-form-urlencoded',
                'Accept':'*/*',

                },
                data : {
                    username : '******',
                    password : '*****',
                    output_mode : 'json'
                }
        };

    app.controller('myController', function($scope, $http) {
        $http(req).success(function(response){

                        $scope.sessionKey =response.sessionKey;
                            });
    });

    </script>

</html>

Please guide me as I am unable to resolve this. Already read about CORS and added [httpServer]
crossOriginSharingPolicy = http://localhost:8080
to server.conf. Also please suggest if something more has to be done.

I am deploying the application on Tomcat and Splunkd is running on default port 8089 (OS:Win7).

I tried it from Java console application and it works like butter.. Kindly help me out with the error.

Thanks and Regards.

Communicator

There are two probable solutions to this problem. First of all this is CORS problem, means the browser will stop the request to send the request to Splunk server as the splunk is not authorized to response to different domains.
Solution:

  1. You have to add the Allow-Control headers in splunk, check out server.conf and web.conf. for documentation.

  2. If you want to bypass the splunk config , and don't want to touch the web.conf files , then make a local request to application server and from there you can make a request to Splunk server. You will by pass the browser restrictions.Also you have to keep in mind that your management port is running on valid certificate ,otherwise at the application server side of your application you have to allow the unsecure connections(I got the same problem when i was connecting from node).

0 Karma

Explorer

If this issue hasn't been solved:

It means you're creating a request to a domain which is not your angular app's host domain. This is called a cross-domain-request which you need to fix server side, not in your angular app.

The header of your response needs to contain the following:

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Origin: *

You might want to replace * with perhaps the domain of your choice since it may pose security issues in production.

0 Karma

Communicator

Any leads on this .

0 Karma
State of Splunk Careers

Access the Splunk Careers Report to see real data that shows how Splunk mastery increases your value and job satisfaction.

Find out what your skills are worth!