Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
"Method Not Allowed The method is not allowed for the requested URL" even with methods = ["POST"] enabled?

In a render.html file I have a button that calls a function in render.js (I included render.js as src in script)

<button type="button" onclick="someFunc()">Calculate</button>

In render.js someFunc() makes a request

options =  {
        method: "POST",
        headers: {
           "Content-Type" : "application/json"
        },
        body: JSON.stringify(feat1dataObj)
    }

    const test1 = await fetch('http://localhost:5000/analyzeInput', options)

app.py is the backend that should handle the request

from flask import Flask, jsonify, request

app = Flask(__name__);

@app.route('/analyzeInput', methods = ['POST'] )
def analyzeData(res):
    part1Len = request.json['part1Len']
    print(part1Len);
    res = jsonify( {"Testing":"1"} )
    res.headers.add('Access-Control-Allow-Origin', '*')
    res.headers.set('Access-Control-Allow-Methods', 'GET, POST')
    return res;


#start server
if __name__ == "__main__":
    app.run(debug=True);

But I get 405 ""Method Not Allowed The method is not allowed for the requested URL"; the typical solution is that method = ["POST"] is not included but I have that.

GitBash terminal Output

Seems like I'm making a GET request somehow?

Overall, I'm using ElectronJS to make a GUI, just trying to pass data from the GUI to Python script to process, not sure why tutorials recommend hosting; seems like there should be a way a much simpler way to do this.

EDIT:

@app.route('/analyzeInput', methods = ['GET', 'POST', 'OPTIONS'] )
#def analyzeData(res):
#def analyzeData(request):
def analyzeData():
  if request.method == 'POST': 
    print("entered analyzeInput")
    #part1Len = request.json['part1Len']
    #print(part1Len);
    res = jsonify( {"Testing":"1"} )
    res.headers.add('Access-Control-Allow-Origin', '*')
    #res.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
    res.headers.add('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    return res;

Code currently looks like this (I changed analyzeData to analyzeInput; and changed it everywhere so I don't worry about that part). Getting "view function for 'analyzeData' did not return a valid response. The function either returned None or ended without a return statement" as the internal server error.

Again, pretty sure that the problem is that the request is sent as "GET" not "POST" but cannot find an explanation for this. New output

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's cross site origin(CORS) issue. From your code I can see you have already taking care of that but in the app.route add options like this: @app.route('/analyzeInput', methods = ['POST', 'OPTIONS'] ) . The reason is that the browser rewrites the request and changes the request method to options in compliance to the cross site origin(CORS) policy that took effect in 2014.

about 4 years ago · Juan Pablo Isaza Relatório

0

In addition to @john mba’s answer you should add mode: “cors” to your options

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda