missing 'Access-Control-Allow-Origin'


  • Module Developer

    I’ll try to grab some informations from a JSON Data feed.
    But I’ve got always this error:

    Quellübergreifende (Cross-Origin) Anfrage blockiert: Die Gleiche-Quelle-Regel verbietet das Lesen der externen Ressource auf http://beny.ch/ironman/2015. (Grund: CORS-Kopfzeile 'Access-Control-Allow-Origin' fehlt).
    ironman: Could not load data.
    
    console.error(message);
    

    Datalink:
    http://beny.ch/ironman/2015

    here is the code to grab the content:

    		var ironmanRequest = new XMLHttpRequest();
    		ironmanRequest.open("GET", url, true);
    		ironmanRequest.onreadystatechange = function() {
    			if (this.readyState === 4) {
    				if (this.status === 200) {
    					self.processIronman(JSON.parse(this.response));
    				} else if (this.status === 401) {
    
    					self.updateDom(self.config.animationSpeed);
    
    					Log.error(self.name + ": Error forbidden");
    					retry = false;
    				} else {
    					Log.error(self.name + ": Could not load data.");
    				}
    
    				if (retry) {
    					self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
    				}
    			}
    		};                
    		ironmanRequest.send();
    

    How can I fix that?

    Thanks for helping me :-)


  • Moderator

    This comes up quite often, not just here on the MM² forums, but out in the wild too:

    http://lmgtfy.com/?q=Access-Control-Allow-Origin

    Basically the answer is: you can’t do that … cross-domain … security … find proper API … all of the above …


  • Module Developer

    @KirAsh4 Well, not strictly true ;) https://forum.magicmirror.builders/topic/157/mmm-opentherm
    But yes, you’ll need to fix the source!


  • Admin

    Just use a node_helper.js to get around the access-allow-origin policy.


  • Module Developer

    After googleing 1000 different websites I’ve used the nood_helper.js solution.

    And to load the nood_helper.js I had to restart electron. pm2 restart mm
    that coast me another hour to find out why it was no loading :-):gun: :hammer:



  • I am facing the same problem. Could you please give an example, how you used node_helper.js to circumvent the access-allow-origin policy?

    Thanks!


  • Moderator

    Like here for example:
    https://github.com/yawnsde/MMM-RNV

    You use request instead.



  • Thank you!

    And why does it have to be in node_helper, not in the main script? Sorry, if this question sounds stupid, I’m new to developing modules.

    Also, is there a reason for declaring var self = this; and using self.func instead of this.func? I have seen this quite a bit and couldn’t understand why people do this…


  • Moderator

    @KrawallKurt
    Please note I am not an experienced javascript developer either. When using addon modules/libraries like request you use the node_helper.js to load this library. I am not sure if it is not possible at all to do so in the main module file or if it is just complicated. Maybe @strawberry-3-141 can clarify :)

    self = this was used in other modules and I added it to my module as well, because I did not want to possibly break something by leaving it out. There is a lot of discussion on the web why this is used and if this still should be used.



  • Concerning the self=this question, I found a neat article giving som explanations: https://alistapart.com/article/getoutbindingsituations

    The other question: Could it be, that node_helper is executed on the server and the main script is executed on the client?


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.