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 Commented Nov 10, 2020 at 16:38
  • @zainul please check the answer Commented Nov 11, 2020 at 4:18
  • Thanks for the answer Commented Nov 11, 2020 at 6:20
  • @zainul If the answer helped you resolve your error. Kindly upvote it. So it will help other people as well. Commented Nov 11, 2020 at 10:36

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.