2021-04-27

Javascript Communicating with Python

I am attempting to send data from a Javascript program to a python program using python -m http.server as a means to run it locally. When I run it, the error message I get is: [HTTP/1.1 501 Unsupported method ('POST') Here is the useful portion of my javascript:

document.addEventListener("DOMContentLoaded",() => {

  document.querySelector("#search_button").addEventListener("click", (e) => {
    document.getElementById("company").innerHTML = document.getElementById("search").value;
  });

  document.querySelector("#week").addEventListener("click", (e) => {
    var time = 1;
    fetch("/yahooFinanceAPI.py", {
      method: 'POST',
      headers:{'Content': 'application/JSON'},
      body: JSON.stringify({
        'Company' : document.getElementById("search").value,
        'Time' : time
      })
    }).then(function(response)
    {
      console.log(document.getElementById("search").value);
    })
  });

Here is the useful python:

@bp.route('/SecurityBenefit.js', methods = ['POST'])
def get_post_javascript_data():
    if request.method == 'POST':
        data = request.get_json()
        ticker = data['Company']
        timespan = data['Time']
        print (ticker)
        print (timespan)
    return getPlot(ticker, timespan)

Does the python program need to run in the background for the program to register? Do I need to implement a do_POST function in my python?



from Recent Questions - Stack Overflow https://ift.tt/3vjMEBM
https://ift.tt/eA8V8J

No comments:

Post a Comment