Splunk Dev

How to read Ajax passed data in Python?

kasu_praveen
Communicator

Hi,

How to read Ajax passed data in .py file? Below are my code snippets from Splunk view, web.conf and .py file.
Looking for code snippet in How to read ajax passed data here????? section of my .py file

JavaScript:

        function callScript(){
            $.ajax({
                  type: "POST",
                  url: "/custom/MyApp/my_script/my_endpoint",
                  data: {'name': 'ABC'},
                  success: function(data) {
                      console.log("success returned");
                      console.log(data);
                  }
            });
        }       
        callScript();

web.conf

[endpoint:my_script]

my_script.py

import logging
import cherrypy
import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.util import make_splunkhome_path
from splunk.appserver.mrsparkle.lib.decorators import expose_page

_APPNAME = 'MyApp'
def setup_logger(level):
 logger = logging.getLogger('splunk.appserver.%s.controllers.my_script' % _APPNAME)
 logger.propagate = False  # Prevent the log messages from being duplicated in the python.log file
 logger.setLevel(level)
 file_handler = logging.handlers.RotatingFileHandler(
     make_splunkhome_path(['var', 'log', 'splunk', 'my_script_controller.log']), maxBytes=25000000, backupCount=5)
 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
 file_handler.setFormatter(formatter)
 logger.addHandler(file_handler)
 return logger
logger = setup_logger(logging.DEBUG)

class my_script(controllers.BaseController):
 @expose_page(must_login=True)
 def my_endpoint(self, **kwargs):
  cherrypy.response.headers['Content-Type'] = 'text/plain'
<<<<<<How to read ajax passed data here?????>>>>>>>
  return "Return text from Python code"
Tags (3)
0 Karma
1 Solution

kasu_praveen
Communicator

I got solution on my own. logger.debug(kwargs['name']) works for me.

View solution in original post

0 Karma

kasu_praveen
Communicator

I got solution on my own. logger.debug(kwargs['name']) works for me.

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...