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.

    What's the best way to fetch JSON data?

    Scheduled Pinned Locked Moved Solved Development
    6 Posts 2 Posters 1.4k Views 2 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.
    • C Offline
      CreepinJesus
      last edited by

      Hi all,

      I’m hoping some of your developer knowledge can help me out here.

      What is the best/recommended way to retrieve JSON (or other) data from external APIs? Up to now I’ve been using fetch() in the main module .js file, but after hitting some CORS snags, I’m wondering if there is a better method?

      Thanks.

      S 1 Reply Last reply Reply Quote 0
      • C Offline
        CreepinJesus
        last edited by

        That didn’t help, but I’ve fixed the CORS issue by allowing requests from that origin on the server in question anyway.

        Kind of digressing from the original question but if fetch does the job then I suppose it’s good enough!

        1 Reply Last reply Reply Quote 1
        • S Offline
          sdetweil @CreepinJesus
          last edited by sdetweil

          @creepinjesus cors has nothing to do w fetch. request and axios will have the same problems.
          thats the server side protecting itself

          i generally dont use the builtin .json() methods provided by fetch and others, cause i dont like the error handling of the actual data transfer part.

          i use JSON.parse() with try/catch around it

          or JSON.stringify() to get the json text

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          C 1 Reply Last reply Reply Quote 0
          • C Offline
            CreepinJesus @sdetweil
            last edited by

            @sdetweil said in What's the best way to fetch JSON data?:

            @creepinjesus cors has nothing to do w fetch

            No, that’s fair enough. Just checking it’s not the fetch method itself.

            Is it possible then to even access JSON that’s being blocked by CORS using some other method? Would making the request from node_helper make a difference?

            The data is being served on a local server and I can use curl or a browser to access it without issue; just not from a script in the browser (of course).

            S 1 Reply Last reply Reply Quote 0
            • S Offline
              sdetweil @CreepinJesus
              last edited by

              @creepinjesus is the address: in config.js “localhost”?

              try changing to “0.0.0.0”

              or vice versa

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • C Offline
                CreepinJesus
                last edited by

                That didn’t help, but I’ve fixed the CORS issue by allowing requests from that origin on the server in question anyway.

                Kind of digressing from the original question but if fetch does the job then I suppose it’s good enough!

                1 Reply Last reply Reply Quote 1
                • S Offline
                  sdetweil
                  last edited by

                  @creepinjesus when the server is remote there are urls u can use to proxy your real url

                  search for cors proxy

                  Sam

                  How to add modules

                  learning how to use browser developers window for css changes

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    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