Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch api #198

Open
s-mostafa-d opened this issue Oct 24, 2021 · 0 comments
Open

fetch api #198

s-mostafa-d opened this issue Oct 24, 2021 · 0 comments

Comments

@s-mostafa-d
Copy link

Hello guys, I am working on json lottie and I wrote a color editor for these files with JavaScript and now I want this editor on the site page to be connected to the lottie file with the API, and since the code for lottie files is very large, for example, Take the color key to changes the color I searched the internet a lot to find my problem and I realized it can be done with fetch functions I found a code on the Internet but it only loads the file on the site console I want to change the value and the user download the new color If my explanation was vague, the site https://lordicon.com/icons is exactly my explanation. I appreciate any help in this regard. This is my code.

     <script>
          const getBtn = document.getElementById('get-btn');
          const postBtn = document.getElementById('post-btn');
          const sendHttpRequest = (method, url, data) => {
            return fetch(url, {
              method: method,
              body: JSON.stringify(data),
              headers: data ? { 'Content-Type': 'application/json' } : {}
            }).then(response => {
              if (response.status >= 400) {
                // !response.ok
                return response.json().then(errResData => {
                  const error = new Error('Something went wrong!');
                  error.data = errResData;
                  throw error;
                });
              }
              return response.json();
            });
          };
          const getData = () => {
            sendHttpRequest('GET', 'https://maycoweb.com/library/2021/10/warm.json').then(responseData => {
              console.log(responseData);
            });
          };
          const sendData = () => {
            sendHttpRequest('POST', 'https://maycoweb.com/library/2021/10/warm.json', {
              email: 'eve.holt@reqres.in'
              // password: 'pistol'
            })
              .then(responseData => {
                console.log(responseData);
              })
              .catch(err => {
                console.log(err, err.data);
              });
          };
          getBtn.addEventListener('click', getData);
          postBtn.addEventListener('click', sendData);
          </script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant