• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

call API (no CORS), used to do it with php proxy

Scheduled Pinned Locked Moved Development
12 Posts 3 Posters 11.2k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    strawberry 3.141 Project Sponsor Module Developer @doubleT
    last edited by Dec 30, 2017, 8:33 AM

    @doubleT if you do it in the node_helper with the request module you shouldn’T have an issue with cors. Try something like:

    var request = require('request'); // at the top of the file
    
    ...
    
    getData: function(payload) {
         request('http://apisource.de/api/getPrices.php?id=12300123', (error, response, body) => {
                if (response.statusCode === 200) {
                    const parsedBody = JSON.parse(body);
                    this.sendSocketNotification("MSG", parsedBody);
                } else {
                    console.log(`Error getting price data ${response.statusCode}`);
                }
            });
    }
    

    Please create a github issue if you need help, so I can keep track

    1 Reply Last reply Reply Quote 0
    • D Offline
      doubleT Module Developer
      last edited by doubleT Aug 7, 2018, 2:47 PM Dec 30, 2017, 5:52 PM

      Sorry, but that doesn’t work. (It throws response.statusCode = 403, forbidden.) And it’s not surprising. As I said, the API source server doesn’t allow CORS and is not serving JSONP. So JavaScript calls are blocked.

      There are a lot of fine tools for specific jobs, XMLHttpRequest, fetch, request, fs, …, and they work if CORS is set up correctly on the server, allowing you access, or it’s giving you JSONP to handle, but PHP’s file_get_contents is the hammer in your toolbox. If everything else fails, you still can throw this at your problem (provided you have allow_url_fopen).

      And I know, it’s not always wise to use (or even throw) a hammer, access might be forbidden (to scripts) for a reason. But if you can read it in your browser, PHP can read, stringify and proxy it to your JS.

      1 Reply Last reply Reply Quote 0
      • 1
      • 2
      • 2 / 2
      2 / 2
      • First post
        11/12
        Last post
      Enjoying MagicMirror? Please consider a donation!
      MagicMirror created by Michael Teeuw.
      Forum managed by Sam, technical setup by Karsten.
      This forum is using NodeBB as its core | Contributors
      Contact | Privacy Policy