I’m trying to get a simple http server going to do an assignment for a Udacity course on CSS. The snag is, doing the assignment involves enabling the server to allow Cross-Origin Resource Sharing (CORS) to allow Udacity’s grading script to mark it.
Following the instructions at https://www.swi-prolog.org/pldoc/man?section=httpcors I’ve come up with the code below, but that doesn’t overcome the Udacity grading script’s “The linked JSON page isn’t at the same origin and directory as the document” error message.
Because CORS is a security risc (see references), it is disabled by default. It is enabled through the setting http:cors. The value of this setting is a list of domains that are allowed to access the service. Because * is used as a wildcard match, the value [*] allows access from anywhere.
So, you need to use the setting interface by either loading a setting database or calling set_setting_default/2 in the application.
You have to be a bit careful using set_setting/2 wrt ordering. So, maybe this wasn’t picked up. Try running ?- setting(http:cors, X). to see whether it did its job. set_setting_default/2 is AFAIK more robust when used as a directive.
Note that you also did not cors enable /images_src and /css this way (but that might be intentional).
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
But Udacity’s script still isn’t happy, so that’s where the problem seems to be.
I’ve zero prior experience with CORS, and since I’m just doing the free CSS course for my own experience (not worried about marks), I think I’ll give up on autograding. The code in the supplied index.html file looks like this, and I’m not sure if I’m supposed to configure for it somehow in the server code:
Their instructor notes simply tell students to create their own simple server with a link to an out-of date Python example, and I gather from searching slashdot that getting CORS to work is a common frustration irrespective of what programming language one uses for a basic http server.