API in Component not working after Deployment
Question
Block.js =>
const bm = editor.BlockManager;
const canvas = editor.Canvas
bm.add('comp-block', {
label: 'Component',
category: 'Extra',
content: {
type: 'component-test'
},
attributes: {
class: 'fa fa-bars'
},
});
Component.js =>
editor.DomComponents.addType('component-test', {
model: {
defaults: {
endpoint: ' // API URL // ',
traits: [
{
type: 'select',
name: 'endpoint',
changeProp: 1,
options: ['endpoint'],
}
]
},
init() {
this.listenTo(this, 'change:endpoint', this.refreshContent);
this.refreshContent();
},
refreshContent() {
const { endpoint } = this.attributes;
const response = axios.get(endpoint,
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
var result= // API Response //
this.components(result);
}
}
});
We are able to call the API and it works in Deployment when we are passing the whole html with script, body, style in Content attribute of Block Manager
Content :
<script>
var request = new XMLHttpRequest()
request.open('GET',' // API URL // ', true)
request.setRequestHeader("Content-type", "application/json;charset=UTF-8");
request.setRequestHeader("Accept", "application/json, text/plain, */*");
request.onload = function(){
var jsonData = JSON.parse(this.response)
request.send()
</script>
But when the API is called through the Component it's not working when the page loads in deployment. It only works when i drop it on the editor and not when it loads as a deployed url.
Ex:
model: {
defaults: {
endpoint: ' API URL ', // some prop for the trait
traits: [
{
type: 'select',
name: 'endpoint',
changeProp: 1,
options: [ 'endpoint' ],
}
]
},
init() {
this.listenTo(this, 'change:endpoint', this.refreshContent);
this.refreshContent();
},
async refreshContent() {
const { endpoint } = this.attributes;
const response = await fetch(endpoint);
...
let result = '';
// ... HTML result from the async response
...
this.components(result);
}
}
Answers (3)
It only works when i drop it on the editor and not when it loads as a deployed url
If you load stuff from some kind of storage (eg. DB) in your "deployed url", be sure to have a correct remote setup
@artf I understand your concern but in the deployed version API calls inside blockmanager content are working only the component api's are not getting called. So is it a problem with remote setup or something else ?
KIndly help me regarding the issue ... Thanks in Advance!
So is it a problem with remote setup or something else ?
Yeah, as you described it might be an incorrect storage setup (check also errors/warning in console)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3207
Form is not submitting
I am trying to create as custom block. But the form is not submitting when i use components inside content. I am able to get all the form d...
Issue #1584
Not getting the Script of Block from editor.getComponents()
Below is the Component that I'm adding. this.editor.BlockManager.add('dob-mmyy-pii', { id: 'dob-mmyy', label: DOB (MM, YYYY) (PII), categor...
Issue #1034
insert element or block inside parent element
Good day, i have component, have block i have Trait on listen changes, how can I add "h1 " inside that block "'<div class="container" style...
Issue #2670
[QUESTIONS] Doesn't appear HTML class name of a custom block on editor
I added my block like this below Then drag this block and drop to canvas, and show HTML code with export-template command. 👆OK, this is wh...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.