Using jQuery, I dynamically replace div's content with form which contains two textboxes and a button. After clicking this button, ajax is used to get results from public webservice according to data collected from textboxes. The code:
$(document).ready(function () {
$(document).on('submit', '#formId', function (e) {
e.preventDefault();
$("#formId").on("click", "#btnShowCurrency", function (event) {
var currencyCode = $('#txtCode').val();
var currencyDate = $('#txtDate').val();
var urlWithVar = 'http://api.nbp.pl/api/exchangerates/rates/a/' + currencyCode + '/' + currencyDate + '/?format=json';
var urlWithoutVar = 'http://api.nbp.pl/api/exchangerates/rates/a/usd/2018-03-26/?format=json';
$.ajax({
url: urlWithVar,
type: "GET",
dataType: "json",
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);},
success: function (parsed_json) {
var currency = parsed_json['currency'];
var code = parsed_json['code'];
var mid = parsed_json['rates']['0']['mid'];
var effectiveDate = parsed_json['rates']['0']['effectiveDate'];
alert("Average exchange rate for: " + currency + " [" + code + "]" + ", day: " + effectiveDate + ", is: " + mid);}
});
});
});
});
So, when I use hardcoded urlWithoutVar - it works without problems. But when urlWithVar is used instead - 404 NotFound error is raised. I'm confused, can anyone help?