I want to be able to query a database from Javascript ran on another site.
I’ve spent hours trying to fix this error:
The Javascript that’s requesting the data:
Query('8thtest*', console.log);
function Query(username, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", 'https://scratchfoxbot.wixsite.com/epiccoins/_functions/query/' + username, true); //true for asynchronous
xmlHttp.setRequestHeader('Content-Type', 'application/javascript');
xmlHttp.send(null);
}
The http-functions.js file in the backend of my wix site:
import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
export function get_query(request)
{
let options = {"headers": {
"Access-Control-Allow-Origin": "https://www.jdoodle.com/",
"Content-Type": "application/javascript"
}};
return wixData.query("Epic_Coins")
.hasAll("account", "request.path[0]")
.find()
.then( (results) => {
// matching items were found
if(results.items.length > 0) {
options.body = {
"items": results.items
};
return ok(options);
}
// no matching items found
options.body = {
"error": `${request.path[0]} wasn't found`
};
return notFound(options);
})
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
everything is saved and published
a little help will go a long ways
side note: if you go to https://scratchfoxbot.wixsite.com/epiccoins/_functions/query/8thtest* it will say that it couldn’t find 8thtest* which definitively exists, as you can see by going to https://scratchfoxbot.wixsite.com/epiccoins/Balance