-1

Briefly, I am sending data from a SelectField (WTFORMS) using Ajax to update other SelectField choices dynamically.

i have tried to send data as default or json, in both cases unsuccessfully.

here is the final code i tried:

in the jinja2 template:

[...]
<th width="20%">{{ studsubreg.sub }}</th>
[...]
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript">
$(document).ready(function(){

    $("select#studsubreg-sub").change(function() {



            var subid = $(this).find('option:selected').text();
            {#var subid=$(this).val();#}

            $.ajax({
                type: 'GET',
                data: { 'sid': subid},
                url: "{{ url_for('getcourse') }}",
                contentType: 'application/json;charset=UTF-8',
                success: function(response) {
                }
            });


    });
});


</script>

in Flask: (just to test if the output is working)

@app.route('/getcourse', methods=['GET'])
@login_required
def getcourse():
    subid = request.json['sid']
    print(subid)

i always get error 400 Bad request i just want to get the subid value so i can return the results to populate the other SelectField.

many thanks

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ahmad313
  • 57
  • 6

1 Answers1

1

Try request.args.get('sid').

You are just getting one of the argument using Ajax.

Also remove contentType: 'application/json;charset=UTF-8'.

Jessi
  • 1,378
  • 6
  • 17
  • 37