0

I am calling an Azure Function which is publicly accessible from my SPFx web part.

It is a POST Request and I am getting a Failed to Fetch error.

The same API call is working fine if I execute it from POSTMAN.

Below is the code.

fetch(checkSubscriptionURL, {
          method: "POST",
          body: JSON.stringify({ "TenantID": tenantId }),
          headers: {
            "Content-type": "application/json",
            "Accept": "application/json"
          }
        }).then((res) => {
          console.log(res);
          if (res.status !== 200) {
            reject();
          }
          else {
            res.json().then(data => {
              if (data && data.length > 0) {
                resolve(this.parseSubscription(data[0]));
              }
              else {
                reject();
              }
            })
          }
        }).catch((e) => {
          console.log(e);
          reject();
        })

Can anyone please help with this?

I tried to make the call with SPFxHTTPClient but getting the same error with that as well.

enter image description here

Thanks in advance.

4
  • were you able to resolve it ? I am getting same error but with PnPJs in SPFx Webpart – zainul Nov 10 '20 at 16:38
  • @zainul please check the answer – Litt Spector Nov 11 '20 at 4:18
  • Thanks for the answer – zainul Nov 11 '20 at 6:20
  • @zainul If the answer helped you resolve your error. Kindly upvote it. So it will help other people as well. – Litt Spector Nov 11 '20 at 10:36
0

I resolved the error by setting the value of CORS in the Azure Function API Settings.

This is not available in the New Experience. We can access it from the classic/old view of azure function.

Steps:

  1. Go To the Azure Function.
  2. Switch to classic view.
  3. Click on Platform Features.
  4. Under the API section, click on CORS.
  5. Under Allowed origins add your domain URL.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.