Skip to content

Instantly share code, notes, and snippets.

@apb2006
Last active August 8, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apb2006/ea6a901a570b04f94e0e0e21098a5402 to your computer and use it in GitHub Desktop.
Save apb2006/ea6a901a570b04f94e0e0e21098a5402 to your computer and use it in GitHub Desktop.
BaseX CORS sample
<html>
<head>
<meta charset="utf-8" />
<title>BaseX CORS test (with Vue.js)</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="BaseX CORS test, (with Vue.js)" />
<meta name="author" content="andy bunce" />
</head>
<body>
<h1>BaseX CORS test, (with Vue.js)</h1>
<div id="example">
<input v-model="url" placeholder="endpoint" size="50">
<select v-model="method">
<option disabled value="">Please select method</option>
<option>GET</option>
<option>POST</option>
<option>PUT</option>
</select>
<hr/>
<button :disabled="!method" @click="getName">Run</button>
<pre><code>{{ response }}</code></pre>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
new Vue({
el: '#example',
data: { url: 'http://localhost:8984/test/cors',
method:null,
response: null},
methods: {
async getName(){
const opts = {method: this.method}
const res = await fetch(this.url,opts);
this.response = await res.text();
}
}
})
</script>
</body>
</html>
(:~
: cors test ( using BaseX 9.3)
: @date 2020-08-07
: @author andy bunce
:)
module namespace _= "apb/cors";
(:~ pre build permissive CORS headers :)
declare variable $_:CORS-HEADERS:=web:response-header(
map { },
map {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,PUT' (: @see https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMethodNotFound :)
}
);
declare
%rest:GET %rest:path("/test/cors")
function _:cors-get()
{
(: $_:CORS-HEADERS, :)
_:response()
};
declare
%rest:POST %rest:path("/test/cors")
function _:cors-post()
{
(: $_:CORS-HEADERS, :)
_:response()
};
declare
%rest:PUT %rest:path("/test/cors")
function _:cors-put()
{
$_:CORS-HEADERS,
_:response()
};
declare
%rest:OPTIONS %rest:path("/test/cors")
function _:cors-options()
{
$_:CORS-HEADERS,
_:response()
};
(:dummy response showing client headers :)
declare
function _:response()
{
<you-sent method="{ request:method() }" ts="{ current-dateTime() }" >{
request:header-names()!<header name="{.}">{request:header(.)}</header>
}</you-sent>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment