1

This script

<script type="text/javascript">
$(function() {
$('.send').live('click', 'button', function()
{
var user1 = $(this).val();
var user2=$(this).prev().val();
var text=$(this).prev().prev().val();
var my_data = {
    user1: user1, text:text, user2:user2,
};
console.log(my_data)
$.ajax({
    url: "/updatechat",
    data: my_data,
    type: 'POST',
    success: function(response) {
        console.log(response)
     },
    error: function(error) {
        console.log(error);
    }
});
});

});

is raising an error with the correspondent route /updatechat

@app.route('/updatechat', methods=['GET','POST']) 
def updatechat():
user1 =  request.form['user1']
user2 =  request.form['user2']
text =  request.form['text']
return [user1,user2,text] #not the actual code

but will NOT raise the same error if, in the previous piece of code, i replace with this

user1='bbb'
user2='whatever'
text='idk'

this "var = request.form['var']" form works on several different routes on my code

David Spira
  • 153
  • 2
  • 16
  • `.live` was deprecated ages ago. Use `.on` – hjpotter92 Jun 24 '16 at 08:24
  • What is the error? Please edit your question to include the full error and traceback. – dirn Jun 24 '16 at 12:35
  • i have to use .live because the button is generated dinamically in the process.. I found along the process of coding this project that if i used on, the button wouldn't work. For some reason @hjpotter92 besides. Since the function works with the user1='bbb',user2='whatever', etc, the problem should not be on the .live thing – David Spira Jun 24 '16 at 23:34
  • @dim POST http://192.168.1.52:5000/updatechat 400 (BAD REQUEST) XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "http://192.168.1.52:5000/updatechat..."} – David Spira Jun 24 '16 at 23:37
  • You're not using a form, instead of request.form['user1'] you need request.args.get('user1') – Amin Alaee Jun 25 '16 at 05:45
  • when replacing request.form with request.args.get, i end up with None being assigned to user1, user2, etc.. @MohammadAmin – David Spira Jun 25 '16 at 19:06
  • In your Ajax change data to this : var my_data = { 'user1': user1, 'text':text, 'user2':user2, }; if this doesn't work, try viewing the request in the Network tab of your browser console to make sure you're sending the correct data – Amin Alaee Jun 25 '16 at 19:09
  • https://postimg.org/image/c9znfvf4h/ @MohammadAmin Is this it? – David Spira Jun 25 '16 at 22:01
  • As you can see in the image you're getting it in the form data so request.form.get('user1') should definitely work. – Amin Alaee Jun 26 '16 at 05:16

1 Answers1

0

Solved The proper form was user1 = request.values.get("user1")

source: https://stackoverflow.com/a/20341272

Community
  • 1
  • 1
David Spira
  • 153
  • 2
  • 16