I'm using ajax to send some data on a button press, from the HTML to flask and then from flask to another Python Server. From there the server sends a response back and using that response I wanted to update the html on my page using Ajax. I'm using a success clause but the clause is failing and not giving me an error message. Could someone help point out where the silent failure is?
This is the Javascript for the function when called by the HTML.
and my Python code which sends the jsonify
response that I want the ReceiveUserUpdate
to deal with.
And then Just for good measure this is my HTML
function ButtonAntiRefresh(){
const BuyPrice = $('input[name="BuyPrice"]').val();
const AmountToBuy = $('input[name="AmountToBuy"]').val();
const Buy = "Buy";
let response = $.ajax({
url : "/UserTradeRequest",
type : "POST",
contentType : "application/json; charset=utf-8",
dataType : "json",
data : JSON.stringify({BuyPrice:BuyPrice,AmountToBuy:AmountToBuy,Buy:Buy}),
success: function(){
ReceiveUserUpdate(response)
}
});
}
function ReceiveUserUpdate(event) {
const data = JSON.parse(event.data);
const CashAvailableSpan = document.GetElementById("CashAvailable");
CashAvailableSpan.textContent = 'CashAvailable: ${data.CashAvailable}';
const NoOfSalesTodaySpan = document.GetElementById("NoOfSalesToday");
NoOfSalesTodaySpan.textContent = 'Number of Sales Today: ${data.SaleToday}';
const NoOfSalesTotalSpan = document.GetElementById("NoOfSalesTotal");
NoOfSalesTotalSpan.textContent = 'Numberer of Sales Total: ${data.SaleTotal}';
}
@app.route('/UserUpdate')
@login_required
def user_update(stockvalue):
print("There has been a sale")
print(stockvalue)
return jsonify({'CashAvailable':"100.00",'SalesToday':"1245",'SaleTotal':"1245400"})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="post">
<div class="ButtonBuy">
<label for="BuyPrice">Price to buy - £</label>
<input type="number" name="BuyPrice" step="0.01">
<label for="AmountToBuy">Amount to Buy</label>
<input type="number" name="AmountToBuy">
<button type="button" onclick="ButtonAntiRefresh()">Submit</button>
</div>
</form>
<div class="AmountOfCash">
<span id="CashAvailable"></span>
</div>