Issue #2417Opened November 16, 2019by Lakshit-Singhal0 reactions

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)

artfNovember 17, 20190 reactions

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

Lakshit-SinghalNovember 18, 20190 reactions

@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!

artfNovember 19, 20190 reactions

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.