Access-Control-Allow-Origin
Wednesday, 18 January 2017 · 2 messages
LS
lambert strether18 Jan 2017 10:55 #
Hi:
This is probably an extremely newbie-ish question. I'm developing on localhost using MAMP/PHP at port 8888, and basex at port 8984. So I am running into "Cross-Origin Resource Sharing" issues, apparently, when I have the PHP server issue an XMLHttpRequest to the Basex server. I would have thought that this line in my REST GET function
%rest:header-param("Access-Control-Allow-Origin", "http://localhost:8888")
in my *.xqm file would have solved the problem. Instead, I'm getting this error:
400 (Stopped at /Users/shunting/basex/webapp/restxq.xqm, 395/14: [bxerr:BASX0003] Invalid path template: "http://localhost:8888".)
I'm stymied because I don't see how a URL could match a path template in the first place. Is the "Access-Control-Allow-Origin" not a supported parameter? All I want to do is get my two servers on my machine to talk to each other. If this isn't the right way, is there another one? Thank you! * * *
Every communication from this account is off the record, unless explicitly put on the record.
CG
Christian Grün18 Jan 2017 14:03 #
Hi Lambert,
Welcome to the list.
Quoted text · lambert strether · 1 line
> %rest:header-param("Access-Control-Allow-Origin", "http://localhost:8888")
With the %rest:header-param annotation, you can bind header parameters from the client request to variables. An example:
declare
%rest:path("/example1")
%rest:header-param("Access-Control-Allow-Origin", "{$origin}")
function local:example1($origin) {
"Origin: " || $origin
};If you want to create a response with this parameter, you need to build a custom response. In BaseX, you can either use the
web:response-header function for this, or add the XML response created by this function instead (see [1] and [2]): declare
%rest:path("/example2")
function local:example2() {
web:response-header(
map {} ,
map { "Access-Control-Allow-Origin": "http://localhost:8888" }
),
<actual-response/>
};Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Web_Module#web:response-header [2] http://docs.basex.org/wiki/RESTXQ#Custom_Response
Quoted text · lambert strether · 20 lines
> > in my *.xqm file would have solved the problem. Instead, I'm getting this > error: > > 400 (Stopped at /Users/shunting/basex/webapp/restxq.xqm, 395/14: > [bxerr:BASX0003] Invalid path template: "http://localhost:8888".) > > > I'm stymied because I don't see how a URL could match a path template in the > first place. Is the "Access-Control-Allow-Origin" not a supported parameter? > > All I want to do is get my two servers on my machine to talk to each other. > If this isn't the right way, is there another one? > > Thank you! > > * * * > > Every communication from this account is off the record, unless explicitly > put on the record.
Written for BaseX 8.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery