I have a dropdown box which whenever I change its values, a js
script forwards its responses to another dropdown. This script works when is inside the .html file, but once I move it to a seprate .js
file it does not work. this is the code:
$("#id_subtag-tags").change(function () {
var tagId = $(this).val(); // get the selected tag ID from the HTML input
console.log(tagId);
$("#displaytags").html('');
$.ajax({ // initialize an AJAX request
url: '{% url "ajax_load_subtags" %}', // set the url of the request (= localhost:8000/app/ajax/load_subtags/)
data: {
'tags': tagId // add the tag id to the GET parameters
},
success: function (data) { // `data` is the return of the `load_subtags` view function
$("#id_subtag-subtags").html(data); // replace the contents of the subtags input with the data that came from the server
}
});
});
There is another function in the same file which is properly is being loaded to that html file, so I think problem is not in loading. I don't know what is causing this bug.
The error I receive is:
GET failed, ajax_load_subtags 404 (Not Found),
url.py:
path('myapp/post/ajax/ajax_load_subtags', load_subtags, name='ajax_load_subtags'),
My guess is that the problem is in loading the url
, as in html I had {% load static %} but I don't know what is the equivalent in .js
files!!