I need to access some json models in my XBlock javascript file, using the path of this files. I stored the json file in the public directory, as suggested by the local_resource_url() method in the XBlock API.
In the student_view method I add the files as,
model = self.runtime.local_resource_url(self, “public/model.json”)
(…)
Then in the js file I use something like: load("/public/model.json")
I need to get a path to this files to be able to load them in the javascript code. Now I get a 404 error as the request is done to “localhost/public/model.json” and that doesen’t exist.
the full (absolute) URL of that file (as needed for the browser) should be stored in url_to_model, so you have to pass that entire URL to your JavaScript code. Something like this (combine with my answer to your other question):
In python student_view:
data = {
"url_to_model": self.runtime.local_resource_url(self, “public/model.json”),
}
fragment.initialize_js('MyBlock', data)
return fragment
In JavaScript:
function MyBlock(runtime, element, data) {
console.log(`Load from the URL ${data.url_to_model}`);
}
That works for sharing the url between python and js, thanks for that!
However, when I try to load the json file, the GET request returns a 403 forbidden error: GET https://localhost/static/xblock/resources/<my-xblock>/public/(...)/model.json
I think it is due to permissions, but the whole XBlock folder is set to edxapp group.
@braden I have a same types of situation in Xblock. I am trying to send pdfs files from static folder to the front end (HTML) but I am getting the 404 error. Any suggestions or recommendation. Here is my snipet of python code:
def student_view(self, context=None):
"""
The primary view of the DagRagXBlock, shown to students
when viewing courses.
"""
pdfs_urls = [self.runtime.local_resource_url(self, os.path.join('static/input_files', ff)) for ff in self.questions]
print(pdfs_urls)
pdfs_urls = json.dumps(pdfs_urls)
qq_pdfs = {
"pdfs_urls": pdfs_urls
}
html_str = loader.render_django_template('static/html/myxblock.html', qq_pdfs)
frag = Fragment()
frag.add_content(html_str)
frag.add_css(self.resource_string("static/css/myxblock.css"))
frag.add_javascript(self.resource_string("static/js/src/myxblock.js"))
frag.initialize_js('MyXBlock')
return frag
and HTML
<div id="pdfs-container" data-pdfs="{{ pdfs_urls }}"></div>