How to call Ajax on button click in Flask

Viewed 300

I am having an scenario, where in webpage I am having 4 buttons which will call individual form once clicked. In the same scenario, if user open website then it will render the respective template. In which I have 4 radio button and 1 button clicked by dfault. An Ajax call is configured on these buttons but by default those are not getting called in flask. How I can achieve this could you please suggest?

Expected:I have to call Ajax as well as Render the template when user click on same the button. Actual : Flask only renderring the template and not giving ajax call?

Flask:: In below code I have not included secret key and flask app- HTML::

@app.route('/')
@app.route('defaultform')
def abc():
    form = SampleForm()
    return render_template('/abc/xyz.html', form=form)

class SampleForm():
    user_coice = RadioField('radio_field', choices=[('abc','abc'), ('cde',cde'),('xyz','xyz')], 
                            default('abc'))

api.add_resource(testapiclass, '/testapi')

$(document).ready(function(){
    ('#abc').on('click',function(){
        $.ajax({
            type:'post',
            url : 'testapi',
            success:function(data){
            some_operation;
       }

   }
});
 <button class="btn" 
 onclick="window.location.href='{{url_for('defaultform') 
 }} >defaultform</button>
 <button class="btn" onclick="window.location.href='{{ 
 url_for('oneform') }} >oneform</button>
 <button class="btn" onclick="window.location.href='{{ 
 url_for('secondform')}} >secondform</button>
 <button class="btn" onclick="window.location.href='{{ 
 url_for('thirdform') }} >thirdform</button>
0 Answers
Related