Thursday, 7 June 2018

jquery - Why multiple ajax requests in each other do not execute?

I have multiple ajax functions inside each other, but only the first ajax request is executed and I do not know why! I have added some alert fields to see how far it gets.
I am using django environment for this.
So for example when the bidnow-BTN is clicked this jquery should execute, but for some reason the after the first ajax request the page actually refreshes and adds something like
/?bidPrice=100001 to the end of the url of the page. This should not happen.



$("#bidnow-BTN").click(function () {
var varurl = document.URL
var itemId = varurl.split(/\//)[4]


$.ajax({
url: "{% url 'User ID' %} ",
method: "GET",
success: function (data) {
var varurl = document.URL
var itemId = varurl.split(/\//)[4]
var username = data.username
alert("Got here: " + username)

$.ajax({

url: "{% url 'Bidding ID' %} ",
method: "GET",
success: function (data) {
alert("Does NOT reach this point")
for (var i = 0; i < data.users.length; i++) {
if (data.users[i].username == username) {
var id = data.users[i].id
}
else {


}
}


$.ajax({ // kinda checked
url: "{% url 'List Items' %} ",
method: "GET",
success: function (data) {
var varurl = document.URL
var itemId = varurl.split(/\//)[4]

for (var i = 0; i < data.items.length; i++) {
if (itemId == data.items[i].id) {
var currentPrice = data.items[i].higestBid
}
else {

}
if (parseFloat($('#bidAmount').val()) <= currentPrice) {
alert("Please enter a higher amount")
abort()

}

if (parseFloat($('#bidAmount').val()) > currentPrice) {

var post_data = {
'itemId': itemId,
'userID': id,
'bid': (parseFloat($('#bidAmount').val()) || 0)
};


$.ajax({ //checked
url: "{% url 'Modify Bid' %} ",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
method: "PUT",
data: JSON.stringify(post_data),
success: function (data) {
$('#ItemCurrentPrice').empty()
$('#ItemCurrentPrice').append("£" + data.highestBid)






$.ajax({ //checked
url: "{% url 'Bidding ID' %} ",
method: "GET",
success: function (data) {
for (var i = 0; i < data.users.length; i++) {
if (data.users[i].username == username) {

var id = data.users[i].id
}
else {

}
}
var post_data = {
'itemId': itemId,
'userID': id,
'bid': (parseFloat($('#bidAmount').val()) || 0)

};
$.ajax( //checked
{
url: "{% url 'List Bid' %} ",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
method: "PUT",
data: JSON.stringify(post_data),
success: function (data) {


}
}) //Checked




}
}) //Checked

}

}) //Checked
}
}
}
})

}
})
}
})

})

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...