been spending alot of time trying to figure out a way to transfer dynamic data from Jquery to my Python 2.7's SimpleHTTP server. I am new to python and learning as I go.
Tried following many different guides online but im always met with the same error: [HTTP/1.0 501 Unsupported method ('POST') 4ms]
when I try to run the following ajax:
var data = {
data: JSON.stringify({
"location": location
})
};
$.ajax({
url: "/",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: data,
success: function(msg) {
console.log(msg);
}
});
Here is what my Python script looks like:
from flask import Flask
from flask_cors import CORS, cross_origin
from flask import request
from flask import render_template
from flask import redirect, url_for
from flask import jsonify
from flask import json
import subprocess
import os
import sys
app = Flask(__name__)
CORS(app)
def do_OPTIONS(self):
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'POST','GET, OPTIONS')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
@app.route("/", methods=['POST'])
@cross_origin()
def get_data():
data = json.loads(request.form.get('data'))
location = data['location']
return str(location)
print('Current Location: ' + location)
print('Restarting Server...')
workDir = os.path.dirname(os.path.realpath(sys.argv[0]))
subprocess.Popen([workDir + r'\runMap.bat', location], creationflags = subprocess.CREATE_NEW_CONSOLE)
@app.route('/')
def index():
return render_template('index.html')
return str(location)
print('Current Location: ' + location)
print('Restarting Server...')
workDir = os.path.dirname(os.path.realpath(sys.argv[0]))
subprocess.Popen([workDir + r'\runMap.bat', location], creationflags = subprocess.CREATE_NEW_CONSOLE)
I honestly feel like I tried everything, i tried: installing Flask-CORS - Does nothing I tried many different varations of the ajax - No luck Tries the suggestion here - CORS with python baseHTTPserver 501 (Unsupported method ('OPTIONS')) in chrome
I might be missing something obvious here, what do you think im missing?