API in Component not working after Deployment
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
Read full answer below ↓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...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
GrapesJS Inline RTE Plugins Update: CKEditor 5 v0.1.4 and Froala Inline Text Editor
CKEditor 5 Inline for GrapesJS v0.1.4 fixes Studio SDK toolbar clipping, iframe injection and link balloon bugs. Compare with Froala Inline — both $69.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.