Hi,
I want to do the post api call from babylon, before that i want to save my details as a json format in my babylon script using array or list. Depends on the input i will add or delete those data.
Then the final data will be go from the babylon POST api.
Babylon is just Javascript. So you should be able to send Http requests in Babylon just the way you would in any plain JS script :
Gather data
Listen to an event
Send a request with xhttp with the stringified data
It could look something like this :
const myData = {
name: "example",
content: "this data has been set somehow in the Babylon app",
target: undefined, // will be set later
}
// Here you fire the request, based on any Babylon event, for example a pointerDown (mySphere has been declared earlier in the app
mySphere.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickUpTrigger, function () {
myData.target = mySphere
xhttp.open("POST", "https://my.endpoint/custom", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(json.stringify(myData);
}));
So, CORS error is a notorious pain in the butt, when trying to developing http requests, although it has nothing to do with Babylon.js.
The error is raised because the endpoint you are targeting doesn’t know where the request is coming from and wether the source can be trusted. You have to set your trusted domains in your azure server.
I don’t know azure at all, but you should look into that. I found something here, for instance.
CORS setup is done in azure, when i do the post call from postman it works fine, but when i do from babylon its throwing an error.
Please correct me if i am wrong.
It’s possible that Postman is a trusted domain by default for Azure, that may explain it. But localhost definitely needs a CORS settings on the server.