I have a form with a data-attribute containing a URL to an API JSON file:
<form class="product" action="#" data-url="dist/scripts/main.js">
[...]
</form>
I want to pass the URL from the data attribute into an Ajax call in an external script.
external.js:
var apiUrl = $('.product').data('url');
console.log(apiUrl) // This returns the correct URL set above
$.ajax(apiUrl).done(function(data) {
[...]
});
I even condensed it like this and same result:
$.ajax($('.product').data('url')).done(function(data) {
[...]
});
When I do this, my doing a feedback loop, possibly because of the (data) parameter that's being used in the ajax function.
Error: Cannot read property '0' of undefined referring to a line that contains currentPosition = data.Positions[0].Position;
I'm not sure why the URL isn't passing to the ajax function correctly.
Uncaught TypeError: Cannot read property '0' of undefined(…)which is referring to a line that contains this:currentPosition = data.Positions[0].Position;. I don't understand why this could be throwing an error.