network error on http-req extension
Closed this issue · 4 comments
The example provided for the http-req extension does not run completely due to an error: "Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https:://foo.com'" when the message is posted. As a consequence any NetLogo code after the post command does not run.
Maybe this is because http-req uses the post request in synchronous mode? (synchronous mode requests on the main thread are not supported by some browsers).
@akshay-krishnan It's possible it's the synchronous call, what browser are you using? I'm a little suspicious because that error message doesn't look like what you'd see if the deprecation was the cause.
I tried this in the latest Chrome and it worked fine:
extensions [ http-req ]
to go
show http-req:get "https://raw.githubusercontent.com/NetLogo/Tortoise/master/README.md"
end
The more likely cause of the issue you're seeing is CORS. Any resource you fetch using http-req
must have CORS allowed from the netlogoweb.org
domain (or whichever domain you're using to host your model). The configuration for that will depend on the web server you're using. The example above works because GitHub allows CORS requests to its content from most domains.
@LaCuneta I tried on both Chrome and Firefox.
Is CORS also an issue if both the model and the resource that I am posting to using http-req are hosted on the same machine? (localhost in my case).
The permissions for CORS are set by the web server, but I'm not exactly sure how it'll work with a development localhost setup. If you search localhost CORS request $YOUR__WEB_SERVER
you might get more information.
It also could be some other kind of network issue besides CORS, like a firewall or incorrect port or something. If you're trying to access a resource using http-req
on your localhost with your model running off of netlogoweb.org, I'm guessing that could have a lot of trouble as letting a web browser access a local network address at the direction of a remote domain seems like a security problem. I think it would be more likely to work if your model and resource were posted on a publicly-accessible web server on the internet, or if you ran your own development copy of NetLogo Web locally (if you are already doing this, disregard this advice).
I'm closing this since the sample code I posted does work in both Chrome and Firefox, but feel free to reply if you have trouble.
Thanks @LaCuneta. The problem was indeed the cross-origin request. This was resolved by allowing cross-origin requests on the server.