Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Haga que AJAX publique en el punto final del matraz con un formulario y otros datos

Estoy tratando de enviar dos datos: un formulario y una matriz a través de AJAX a un punto final de Flask. Mi javascript con solo enviar el formulario está aquí:

 $(document).on('click', '#submit_button', function(){ let form_data = $('#form').serialize(); let other_data = updateOtherData(); req = $.ajax({ url : url_final, type : 'POST', data : form_data }); req.done(function(data){ //does some updates to page here }); });

Y en el punto final de mi matraz, esto funciona bien haciendo algo como:

 @app.route('/process_form', methods=['GET', 'POST']) def process_form(): form = MyForm() if request.method == 'POST' and form.validate_on_submit(): #do something here return some_data_back

Sin embargo, me gustaría enviar other_data junto con form_data . Así que he intentado:

 $(document).on('click', '#submit_button', function(){ let form_data = $('#form').serialize(); let other_data = updateOtherData(); req = $.ajax({ url : url_final, type : 'POST', data : {'form': form_data, 'other': JSON.stringify(other_data)} }); req.done(function(data){ //does some updates to page here }); });

Y actualicé mi punto final a algo como:

 @app.route('/process_form', methods=['GET', 'POST']) def process_form(): form = MyForm() if request.method == 'POST': form = #Turn this back to a FlaskForm object other_data = #Just get this as a string if request.method == 'POST' and form.validate_on_submit(): #do something here return some_data_back

He intentado muchas partes diferentes en lo anterior donde no puedo hacer que funcione. Probé request.form, request.data y request.get_json (en el que configuré contentType en la llamada ajax).

He analizado muchas preguntas similares, como: Obtener los datos recibidos en una solicitud de Flask y Obtener el cuerpo POST sin procesar en Python Flask, independientemente del encabezado de tipo de contenido que parecía muy prometedor, pero todo parecía vacío.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simplemente puede usar 'request.json'

 @app.route('/process_form', methods=['GET', 'POST']) def process_form(): form = MyForm() if request.method == 'POST': content = request.json form = content['form'] other_data = content['other'] if request.method == 'POST' and form.validate_on_submit(): #do something here return some_data_back
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!