0

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?

Community
  • 1
  • 1
Daniel Ellison
  • 1,339
  • 4
  • 27
  • 49
  • 1
    Why are you using `SimpleHTTP` rather than `app.run()`? Also you have a duplicate route (`/`) defined and the second definition only accepts `GET` requests – Suever Jul 23 '16 at 03:00
  • I am not familiar with Python at all to be honest, just started 2 days ago. Not familiar with app.run() to be honest with you. I am building a website that contains a google map and looking to transfer the coordonates of a player to python to process afterwards. To be more specific, I am working off this source code https://github.com/earshel/PokeGOMAPSd. – Daniel Ellison Jul 23 '16 at 03:04
  • 1
    Well `app` is your flask app instance, so to run it you can just call `app.run()` at the end of your source. Also get rid of your duplicate route by making one route that accepts `GET` and `POST` or rename one or the other to something like `@app.route('/post', methods=['POST',])` – Suever Jul 23 '16 at 03:06
  • The suggestions by Suever are right on the money. You need to give a different route to your post method. Because the problem is that your AJAX is calling the index view because it has the same route and that view doesn't accept POST – Aquiles Jul 23 '16 at 03:15
  • On top of that, well when you use return the function exits, so using return and then placing other lines of code is an error since those lines will never be run – Aquiles Jul 23 '16 at 03:17
  • Thank guys ill giving Seuver tips a try, so far im getting different results which is a good thing. – Daniel Ellison Jul 23 '16 at 03:21

0 Answers0