MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord
    MagicMirror² v2.24.0 is available! For more information about this release, check out this topic.

    SOLVED What's the best way to fetch JSON data?

    Development
    2
    6
    767
    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
      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
        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
          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

          Create a working config
          How to add modules

          C 1 Reply Last reply Reply Quote 0
          • C
            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
              sdetweil @CreepinJesus last edited by

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

              try changing to “0.0.0.0”

              or vice versa

              Sam

              Create a working config
              How to add modules

              1 Reply Last reply Reply Quote 0
              • C
                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
                  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

                  Create a working config
                  How to add modules

                  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 Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy