Difusio
Difusio

Reputation: 799

Javascript/python design assistance

I'm running an external website that harvests data from an iframe on our internal company intranet. This part is written in Javascript and is working. However, now I need access to the data the js is harvesting to process it further in python. It is not an option to process in Javascript as well as that is technically not possible.

I would appreciate some pointers on where to go from here? How do I get the javascript data into my python scripts?

Upvotes: 0

Views: 52

Answers (1)

Justin Kruse
Justin Kruse

Reputation: 1130

You will need to use an AJAX call to pass the javascript data to your server on the backend for python to process, The best way to do this is to encode your data in a JSON Object and pass it in an AJAX call:

json_obj = {your:[data]};
$.ajax{
    url: [url of webservice/action],
    type: "POST",
    data: json_obj,
    dataType: "json",
    success: function(result) {
     //Write your code here
    }
}
//Note that this does run asynchronously, so the code that follows this will not wait for the ajax call to finish

Python has a nifty module, json, that you can use to decode JSON objects into Python Objects using:

import json

data_obj = json.loads(json_data_string)

This will put the json string you assembled in your javascript then passed to python with AJAX into a Python structure (dictionary or list, depending on how your object was built) for easy python processing.

You can then pass a JSON string back by using the dumps() function:

data_str = json.dumps(data_obj)

Sources:

Upvotes: 1

Related Questions