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

308
Visualizações
ERROR GET 400 (BAD REQUEST) when doing post with ajax (JavaScript) & flask (Python)

I'm trying to do a POST request with ajax on js:

function func() {
    var gameName = document.getElementById("gname").value;
    // var uuid = create_UUID()

    const dataSend ={
        "user input": {
            "game_name": gameName
        }
    }

    console.log("data from user");
    console.log(dataSend);


    $.ajax({
        url: 'http://127.0.0.1:5000/responer',
        contentType: 'application/json',
        cache: false,
        method: 'POST',
        dataType: 'json',
        data: JSON.stringify(dataSend),
        success: function(data) {
            alert.log(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log("ERROR Status:", xhr.status ,"-", thrownError)
        }
        
    });
 };

but I'm getting the error:

127.0.0.1 - - [04/Jul/2022 15:04:17] "OPTIONS /responer?{%22user%20input%22:{%22game_name%22:%22gta%22}}&_=1656936257232 HTTP/1.1" 200 -

127.0.0.1 - - [04/Jul/2022 15:04:17] "GET /responer?{%22user%20input%22:{%22game_name%22:%22gta%22}}&_=1656936257232 HTTP/1.1" 400 -

When I'm working with Postman everything works fine and I get the response:

127.0.0.1 - - [04/Jul/2022 15:01:12] "POST /responer HTTP/1.1" 200 -

My python side using flask:

from flask import Flask, request, jsonify, render_template
from flask_cors import CORS

import main
import json

app = Flask(__name__)
CORS(app)

# cors = CORS(app)
#Create the receiver API POST endpoint:
@app.route("/responer", methods=["POST","GET"])
def postME():
   request_data = request.get_json()
   print("data recived: " + str(request_data))

   res_data = main.response(request_data) #method that returns the response json

   response = app.response_class(
      response=json.dumps(res_data),
      status=200,
      mimetype='application/json'
   )

   return response
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can close the question. My problem was that I added

type:'post'

instead of

method:'POST'

in my ajax request.

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