0

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>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    hi, `GetElementById` should be `getElementById` change same for all . Also you are using `dataType : "json",` so no need to parse return response again. – Swati Mar 10 '21 at 04:10
  • @Swati hey thanks for the feedback. However when i was using console logs to see if it was actually getting anywhere the success was never carried out. Any ideas why? – user11432150 Mar 10 '21 at 12:00
  • are error in browser console ? – Swati Mar 11 '21 at 12:42
  • @Swati no they arnt. Im not seeing any errors anwhere. But i know the success function isnt being called at all – user11432150 Mar 12 '21 at 13:05
  • @user11432150 You can define an [error function](https://stackoverflow.com/a/14563181/9098350). Then you can debug what's going on using this function. – 5eb Mar 12 '21 at 13:14

0 Answers0