This isn't necessarily a web2py-specific question, but perhaps there's a
web2py-specific solution that's preferable to other solutions, so I thought
I'd ask it here.
I have some HTML code and javascript, which I'd like to be able to paste
into any website, and which should access an API endpoint on a web2py
server. The web2py application is running on an nginx server.
The javascript portion of the client code snippet is as follows:
<script>
$(document).on("click", "button", function(e){
...
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://myurl.com/api/action/");
xhr.setRequestHeader("Content-Type",
"application/json");
var jsonStr = JSON.stringify({
Header:{Procedure:"..."},
Body: { ... }
});
xhr.send(jsonStr);
});
</script>
When the button that triggers the above action is clicked, the following
error is generated (in Chrome):
XMLHttpRequest cannot load https://myurl.com/api/action. Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://127.0.0.1:8000' is therefore not allowed access.
As I understand it, this issue can be resolved by enabling CORS in the
nginx settings <http://enable-cors.org/server_nginx.html>. However, I'd
like to limit the modification to that one API function, which corresponds
to the application/controller/function combo, "[MyApplication]/api/action".
I'd appreciate recommendations on how to do this within the context of
web2py running on nginx. Alternatively, is there a modification to the
client javascript code (I'd like to keep the code small, so any
modification would have to not exceed a couple of lines) that would remedy
the problem? Thanks.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.