javascript - Performing ajax with Python Flask -


i'm trying simple yet not able figure out because of no experience working scripting languages , after going through tutorials. closest match want kind of found @ link .

when clicks on following link want value attribute returned same template link i'm creating in can use array's index argument.

<a href="/disable?server={{count + loop.index0}}"  id="servercount"  value="{{ count + loop.index0 }}"> click disable </a> 

the javascript/json/ajax have is:

{% block scripts %}     {{ super() }}     <script type=text/javascript> $(function() {   $('a#servercount').bind('click', function() {     $.getjson('/disable', {       a: document.getelementbyid("servercount").getattribute("value")     }, function(data) {       $("#result").text(data.result);     });     return false;   }); });     </script> {% endblock %} 

i'm sure able realize have not work. i'm posting code i've been experimenting with.

in app.py have:

@app.route('/disable', methods=['get', 'post']) def disablebtn():     valueofbutton = request.args.get['servercount']     return jsonify(result = valueofbutton) 

can explain correct way of doing be? appreciated.

since you're using jquery why not go simple jquery way of getting value?

$('#servercount').val(); 

so typically when make ajax call i'm expecting json data , i'm sending json server. this;

$(function() {     $('a#servercount').bind('click', function() {         $.ajax({             method: "post",             url: "/url/to/your/server/method",             datatype: 'json',             data: { servercount: $('#servercount').val() }             })             .done(function(msgbackfromserver) {                 alert("data sent server" + msgbackfromserver);             });     }); }); 

now you'll notice url needs method can hit on server, sort or page view have. i'm not familiar in flask, imagine it's similar django in ways.

method post because sending data. on flask server, should able receive json , parse python dictionary (or maybe there other flask specific tool).

your response view passed .done function of ajax call. whatever data want be. come flask. i'm sure flask can send json object or string. if it's json object access different parts of . notation. msgbackfromserver.title or msgbackfromserver.message etc...


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -