0

I am trying to consume a Get method from a Webapi in a html page using javascript. I am facing issue during the ajax call to the api. The webapi is working correctly.

I tried searching ways to resolve the issue and most of it leads me to CORS. I have tried options like open with command line flags disabling web security, but have not resolved the issue

<script>

function productList() {
  // Call Web API to get a list of Product
  $.ajax({
    url: 'localhost:3079/api/cities',
    type: 'GET',
    dataType: 'json',
    success: function (products) {
      productListSuccess(products);
    },
    error: function (request, message, error) {
      handleException(request, message, error);
    }
  });
}

function handleException(request, message,
                         error) {
  var msg = "";
  msg += "Code: " + request.status + "\n";
  msg += "Text: " + request.statusText + "\n";
  if (request.responseJSON != null) {
    msg += "Message" +
        request.responseJSON.Message + "\n";
  }
  alert(msg);
}

function productListSuccess(products) {
  // Iterate over the collection of data
  $.each(products, function (index, product) {
    // Add a row to the Product table
    productAddRow(product);
  });
}

$(document).ready(function () {
  productList();
});

</script>


//Webapi Code
 [HttpGet()]
 public IActionResult GetCities()
{
 return Ok(CitiesDatastore.Current.Cities);
}

When i debugged the code using the developer tools, the following error is thrown

Code: 0 Text: error

during the ajax call. How can I resolve the issue

KopMaverick
  • 137
  • 12
  • `url: 'localhost:3079/api/cities',` — Get the URL to your API correct. You are missing the scheme. – Quentin Aug 09 '19 at 10:08
  • "following error is thrown Code: 0 Text: error" — I bet there is a more detailed error reported directly to the Console and not as a result of your `handleException` method. – Quentin Aug 09 '19 at 10:08
  • What do you mean by scheme? I get the json output when i directly run the url in the browser. Can you elaborate – KopMaverick Aug 09 '19 at 10:14
  • `http:` or `https:` – Quentin Aug 09 '19 at 10:14

0 Answers0