XBlocks: Get path of json file stored in public with runtime.local_resource_url()

Hello guys,

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.

Can someone help me solve this problem?

Thanks a lot

When you call

url_to_model = self.runtime.local_resource_url(self, “public/model.json”)

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.

Any suggestion? Thanks

Not sure, sorry. Maybe ./manage.py collectstatic is necessary ?

Based on your suggestion I tried to run the following, which did not work:

python manage.py cms --settings=production collectstatic --noinput
python manage.py lms --settings=production collectstatic --noinput

It looks like the problem is with nginx, which is not able to load the resource. I attach some screenshots.



I tried all I could with permissions, using chown, chgrp and chmod, which also did not work.

I can’t figure out the error