I am currently learning asp.net core 3 and I can’t find any help regarding this issue that I have. I have a form that submits a value with a POST request. But I want the same button to have a GET request that populates another field with a .ajax / xmlhttprequest. But I want the POST method to be executed first and then the GET method. Is it possible to do it? I’ve tried doing it but I got stuck.
These are the methods inside my controller.
JavaScript
x
18
18
1
[HttpGet]
2
public async Task<IActionResult> GetConvertedAmount()
3
{
4
var rate = await _db.ExchangeRates.Where(x => x.Name.Equals(_tM.Currency)).ToListAsync();
5
_tM.convertToCurrency(rate[0].Rate);
6
var amount = _tM.Amount;
7
return Json(amount);
8
}
9
10
[HttpPost]
11
public ActionResult CalculateExchangeRatio(int amount_give, string type_to_give)
12
{
13
_tM.Amount = amount_give;
14
_tM.Currency = type_to_give;
15
return Ok();
16
}
17
18
And this is my JS script
JavaScript
1
13
13
1
$('#calculateButton').on("click", function () {
2
$.ajax({
3
url: "/trade/getconvertedamount",
4
type: "get",
5
success: function (amount) {
6
console.log(amount);
7
alert(amount);
8
}
9
});
10
11
12
})
13
Advertisement
Answer
You can use the $.ajax ‘done’ chaining to complete the entire process:
JavaScript
1
15
15
1
$('#calculateButton').on("click", function () {
2
$.ajax({
3
url: "/trade/calculateexchangeratio",
4
data: { amount_give: 9.99, type_to_give: 'blahblah' },
5
type: "post"
6
})
7
.done(function(){
8
$.ajax({
9
url: "/trade/getconvertedamount",
10
type: "get"
11
})
12
.done(function (amount) { console.log(amount); alert(amount); });
13
});
14
})
15