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