I have 2 methods I want to execute when button or link is clicked One is a Javascript that will send information to a 3rd party and the 2nd one is a backend Django method that will update a field in the database. This way I can control that JavaScript Function will execute only once.
Ideally, Javascript should execute first and only after its execution the backend method will be triggered
I tried to combine it in the same link but this is wrong since only javascript executes
<a href="{% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}" onclick="AddCardToTrello()">Add to Trello</a>
What are my options here ? Can I call my backend method from JavaScript?
UPDATE:
Despite all the great answers the tricky thing in my case how the backed call is generated
{% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}
I use Django as my backend and probably there some difference between invoking the backend method from within JavaScript vs direct click. What happens is that when it is called from within JS domain name gets cut off.
window.location.href = /production/production/order/board/5/127.0.0.1:8000/production/soproduct/details/5/
When the real link is
So somehow when I call the method from Javascript it cuts off my first part of URL(domain name) and this breaks the JS script probably. So none of the answers works. I am not sure about the Ajax since was not able to make it to work so far.