Retrieving data from wix

How do I retrieve data from a wix website via a widget embedded in another website?

Run GET request from the widget.
Have an http function on Wix site. See:
https://www.wix.com/velo/reference/wix-http-functions/get

@jonatandor35 Does it mean https://user.wixsite.com/mysite/_functions/myFunction/John/Doe, is the request sent to the wix site from the widget and would also receive with a variable “x” the payload returned from the request eg. Let x = https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
Does it also mean that from the the content of “options” is an array of objects equal to results.items? Can I actually get a specific item from the array by replacing // return ok(options) with return ok(options[0].fieldname)?

// In http-functions.js

import {ok, notFound, serverError} from ‘wix-http-functions’;
import wixData from ‘wix-data’;

// URL looks like:
// https://www.mysite.com/_functions/myFunction/John/Doe
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/John/Doe
export function get_myFunction(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
// query a collection to find matching items
return wixData.query(“myUsersCollection”)
.eq(“firstName”, request.path[0])
.eq(“lastName”, request.path[1])
.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]} ${request.path[1]}' was not found
};
return notFound(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
“error”: error
};
return serverError(options);
} );
}

@chidiejike hi,
The data you pass should be assign to the options.body.
It can be a json or a string (you should define it in the request headers),

@jonatandor35 Are you saying if I add

"Content-Type": "string" ,

then

return ok(options[0].fieldname)

will be passed back as a string even if it is a number?

@chidiejike

"Content-Type": "text/plain"

and on the receiver side use .text() instead of .json() for the response.

@jonatandor35 Thanks, do you have a sample GET request sample code? because I am getting Cors restriction

@chidiejike Try adding to the headers:

let options = {
    "headers": {
      "Content-Type": "text/plain",
	"Access-Control-Allow-Origin": "*"
    }
  };

and see if it works.

@jonatandor35 It works, Thanks

It has stopped working once again…

@jonatandor35 It has stopped

@chidiejike If it worked and suddenly stopped, I doubt I’ll be able tell what the problem is. It may need some deeper investigation that only Wix Team can do.
Anyway, what do you get? Any error? Anything strange in the browser dev tools > network tab?

@jonatandor35 Same CORS problem “Access to XMLHttpRequest at ‘https://mysite.com/_functions/myFunction/countries’ from origin ‘null’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

@chidiejike if you still have the “Access-Control-Allow-Origin”:""* then I don’t know.
Maybe something is wrong with your function (you didn’t post the function for the function with the countries.

@jonatandor35 It stopped all of a sudden not because I added any new code to the existing one. The country is a request parameter that also Allowed access in the code
If (request.path[0]===“countries”){
let options = {

“headers”:{
“Content-Type”:“application/json”,
“Access-Control-Allow-Origin”:“*”,
}
};
…}

@chidiejike I can’t tell. Maybe it’s in another part of your code, maybe it’s a bug on wix side. I don’t have enough info here.

@jonatandor35 I think it is a bug in wix, maybe you can do quick test.

@jonatandor35 What exactly is wrong with Wix?? Code that worked before stopped working because of CORS error. I decided to disable the browser security now it is given me 404 error, what really is going on with wix?

@chidiejike I really can’t say based on this description. Maybe try contacting Wix support.

@jonatandor35 Wix Support doesn’t handle code related