Search code examples
.netiisxmlhttprequestwebapi

GET not working when trying to access web API


I wrote a simple web API in .net and am running it on a site I created under IIS on Windows 10. If I put the URL into Chrome "https://localhost:64591/api/custom", the JSON delivers the records called from the database. No problem there.

I put that same URL into a XMLHttpRequest object and the onload event never happens. As a test I replaced the URL with an outside URL that I know works. The onload event fires off and the response object works. It seems like there's something going on with IIS and it's blocking the "get" request.

Below is the code. Here's the outside URL that works: https://ghibliapi.herokuapp.com/films ..... There's definitely something up with access to the API through IIS.

var request = new XMLHttpRequest();
request.open('GET', 'https://localhost:64591/api/custom', true)
request.onload = function () 
{
  alert(this.response); 
}
request.send();

Solution

  • Looks like this is a CORS issue. I can perform GET and POST from an html page as long as that page is coming from the same server as my web api and I don't mention the server in the string 'api/custom' as opposed to 'https://localhost:64591/api/custom'. I'm ok with this since I'm just practicing web api stuff. At some point though I have to learn how to configure the CORS module in iis.