简体   繁体   中英

Recieving 401 error on mailchimp api fetch

function emailSubscribe() {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)
     {
        const data = {
            members: [
              {
                email_address: document.getElementsByClassName("email input"),
                status: 'subscribed'
              }
            ]
          };

          const postData = JSON.stringify(data);

          fetch('https://us19.api.mailchimp.com/3.0/lists/mylist', {
            method: 'POST',
            mode: 'no-cors',
            headers: {
              Authorization: 'auth apikey-us19',
            },
            body: postData
          })
            .then(function(response) {
            response.statusCode === 200 ?
            alert("You have been successfully subscribed to StreamIntra. An email should be arriving shortly.") :
            alert("An error has occurred, please try again later.")
            .catch(console.log(response))
            })
           } else {
       alert("You have entered an invalid email address!")
     }
  }

I have this function that I call by a submit button on my local express server but it keeps giving me a 401 error (incorrect api key). I've read the api and check my api key, everything looks fine. I've also tried encoding and sending the key, still nothing. I've looked at this link: MailChimp. Error 401 (Unauthorized) when sending a request for adding a new list member but an answer doesn't seem to be provided. He mentions that mailchimp doesn't work with cors, but I'm running in no-cors mode. Any way to validate my request so I don't get the error?

There is Something wrong with the mailchimp server. Try using firebase instead.

Try Using This. :

function emailSubscribe() {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)
     {
        const data = {
            email: document.getElementsByClassName("email input"),
            [ANY OTHER DATA YOU WANT]...
          };

          const postData = JSON.stringify(data);

          fetch('https://[YOUR ADDRESS].firebaseio.com/', {
            method: 'POST',
            mode: 'no-cors',
            headers: {
              Authorization: '[APIKEY]',
            },
            body: postData
          })
            .then(function(response) {
            response.statusCode === 200 ?
            alert("You have been successfully subscribed to StreamIntra. An email should be arriving shortly.") :
            alert("An error has occurred, please try again later.")
            .catch(console.log(response))
            })
           } else {
       alert("You have entered an invalid email address!")
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM