Dashboards & Visualizations

SDK javascript: client side test (error 404)

LauraBre
Communicator

Hello,

I try to use sdk javascript client side so I've splunk on my host where I add the app JSON2 and I have my project on eclipse where I copy the folder client and jquery.min.js. I want to connect to my local splunk so I have test this:

<html>
  <head>
    <meta charset="utf-8">
    <title>How to start a Splunk session</title>
    <script type="text/javascript" src="scripts/jquery.min.js"></script>
    <script type="text/javascript" src="scripts/client/splunk.js"></script>

    <script type="text/javascript" charset="utf-8">
    function displayApps() {
        var http = new splunkjs.ProxyHttp("/proxy");

        // Create a Service instance and log in 
        var service = new splunkjs.Service(http, {
            username: "admin",
            password: "admin",
            scheme: "https",
            host: "localhost",
            port: "8089"
        });
        // Print installed apps to the console to verify login
        service.apps().fetch(function(err, apps) {
            if (err) {
                alert("Error listing apps");
                return;
            }

            var myapps = "";
            var appsList = apps.list();
            for(var i = 0; i < appsList.length; i++) {
                myapps += "App " + i + ": " + appsList[i].name + "<br/>"
            } 

            document.getElementById("applist").innerHTML=myapps;
        });

    }
    </script> 
</head>
<body>
    <button type="button" onclick="displayApps()">Log in and list apps</button>
    <p id="applist"></p>
</body>
</html>

But I've the alert "Error listing apps" and the error is :

"NetworkError: 404 Introuvable - http://localhost:8080/proxy/services/json/v2/services/auth/login?";
login?
POST http://localhost:8080/proxy/services/json/v2/services/auth/login?  
404 Introuvable

And it's say that it's in jquery.min.js that there is the error and I do'nt know why is localhost:8080 and not 8089?????

Thx by advance if you know where is come from the error.

Laura,

ineeman
Splunk Employee
Splunk Employee

The problem you're having is due to the browser's Single Origin Policy (http://en.wikipedia.org/wiki/Same_origin_policy). The specific issue is that your page is going to be running of yourawesomedomain.com, but Splunk will be running on some separate domain (e.g. yourawesomedomain.com:8089), and the browser does not allow communication to happen between those two.

The solution is to use a server-side proxy to relay the messages. For example, when you run the examples in the SDK, we start up a small webserver that proxies any request to the the "/proxy" endpoint to Splunk. You can similarly have a small proxy in your webserver that does the same.

If you can say a bit more about your environment (what programming language you're using server side, what webserver, etc), I can give a bit more specific advice and help.

azhicodan
New Member

I'm running into the same problem when I run the sample from Visual Studio. Do you have any idea where shall I add similar settings when it comes to IIS?

0 Karma

LauraBre
Communicator

Thx, I did it. I insert my index.html in htdocs of my apache and I add SSLProxyEngine On ProxyPass /proxy/ https://localhost:8089/ in the Apache config file and decommented all modules about proxy. So it works cery well when I doen't use tomcat but when I use tomcat and Apache, there is the error 404 so I don't result the problem with tomcat but I can connect to my spolunk so it's a good thing. Now I want to use this in a dynamic web project where I use tomcat so I search I can I do it? Do you have an idea?

Thx by advance,

Laura

0 Karma

ineeman
Splunk Employee
Splunk Employee

You can try adding the following two lines to your Apache config file:

SSLProxyEngine On
ProxyPass /proxy/ https://localhost:4089/

You will need to change localhost:4089 to wherever your splunkd is (the management host and port). Once you do that, any request that goes to /proxy on your server will be routed to your splunkd installation.

LauraBre
Communicator

Hello,

Thanks very much for your answer. So, if I understand, I have to add a proxy in my apache (I work with a tomcat apache) and eclipse. I program in java. So I create a proxy of my choice. Then what have I to change in my dynamic web java project? And next I just to add the proxy in my browser?

Thx by advance,

Laura

0 Karma
Get Updates on the Splunk Community!

Uncovering Multi-Account Fraud with Splunk Banking Analytics

Last month, I met with a Senior Fraud Analyst at a nationally recognized bank to discuss their recent success ...

Secure Your Future: A Deep Dive into the Compliance and Security Enhancements for the ...

What has been announced?  In the blog, “Preparing your Splunk Environment for OpensSSL3,”we announced the ...

New This Month in Splunk Observability Cloud - Synthetic Monitoring updates, UI ...

This month, we’re delivering several platform, infrastructure, application and digital experience monitoring ...