Calling external API from custom widget

1721
3
03-01-2021 05:44 AM
NishthaKalra
New Contributor II

Can we call an external REST API from custom widget ?

3 Replies
KafilBaig
New Contributor III

Yes, we can call an external REST API from custom widget. I have done in my custom widget. 

For Example the code looks like as below.

 FetchData: function () {

var apiUrl = "https://myservername/myApiname/MyMethodName";

var resultDataItems;

$.ajax({

async: false,

type: "GET",

url: apiUrl,

data: {"strParams": strParams},

success: function (result){

resultDataItems = JSON.parse(result);

}

});

}

 

Now you can bind the result in whatever display component you need in your application , for example Gridview, ListView...

Additionally Add the jquery.min library in your libs folder and reference that in index page.

NishthaKalra
New Contributor II

Thanks @KafilBaig 

I did this but I keep getting CORS error which says that

  1. Access to fetch at <requested API> from origin <my system> has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Do I need to set something in WAB or AGOL to remove this error?

 

 

0 Kudos
KafilBaig
New Contributor III

Try this it might help in resolving the issue.

windows icon -> Right click -> command prompt(Admin)

In command prompt window the type 

C:\Windows\system32>cd .\drivers\etc and enter

C:\Windows\system32\drivers\etc>notepad hosts

A notepad of Hosts opens. Add the dns entry in the file like 

10.11.11.11 maps.org.com

 

0 Kudos