Dashboards & Visualizations

Why am I getting "Error: Http instance required when creating a Context within a browser." when running my JavaScript code?

Communicator

I am getting this error when running my JavaScript code:

Error: Http instance required when creating a Context within a browser. 

I am running the code in the browser. I know I am supposed to have an http implementation, but how do I go about creating that?

Engager

So if http proxy is required why do you provide the code example having no any mention of it like this one?
http://dev.splunk.com/view/javascript-sdk/SP-CAAAEDD

Another issue - even with proxy it doesn't work. In my case the local splunk server is located on different domain, and there is no any description or clue how to provide the right address to it. Because all the data used in object as a second parameter in service.login is not used.

New Member

Hi,

In order to do login request or any other request you have to create http instance.

var http = new splunkjs.ProxyHttp("/proxy");

As per your context this should solve your problem.

0 Karma

Esteemed Legend

You are missing your code.

0 Karma

Communicator

I fixed it but now I am running across a new error. "uncaught exception: No session key available"

The following is my code:

<script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/client/splunk.js"></script>
<script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/jquery.min.js
"></script>

<script type="text/javascript" charset="utf-8">
    var http = new splunkjs.ProxyHttp("/proxy");
    var service = new splunkjs.Service(http,{username: "admin", password: "12334", scheme: "https", host: "megaman", port: "8000", version: "6.2"});
    service.login(function(err, success) {
        if (err) {
            throw err;
        }

        console.log("Login was successful: " + success);
        service.jobs().fetch(function(err, jobs) {
            var jobList = jobs.list();
            for(var i = 0; i < jobList.length; i++) {
                console.log("Job " + i + ": " + jobList[i].sid);
            }
        });
    });

</script>

any help is appreciated.

0 Karma

Communicator

I manage to fix the problem but now I have a new problem. I am trying to use the code from splunk's client side JavaScript and I am getting the error: ReferenceError: displayApps is not defined

the url is:
http://dev.splunk.com/view/javascript-sdk/SP-CAAAEC9

here is my code:

<html>
  <head>
    <meta charset="utf-8">
    <title>How to start a Splunk session</title>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/jquery.min.js"></script>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/client/splunk.js"></script>
    <script type="text/javascript" src="/home/sxiong/Documents/splunk_java_script/splunk-sdk-javascript-master/examples/browser/resources/json2.js">

    <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: "changeme",
            scheme: "https",
            host: "megaman",
            port:"8000",
            version:"6.2.4"
        });


        // Verify we logged in

        // 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 + "  

"
}

            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>

there is some formatting error screwing up the for loop. Just use the like as a reference.

0 Karma