Requests to the API. Why do they work like this?
There is an Api service Kladr. The problem is that when I access the api via the browser's search bar, I get a json response. But when accessing from the script connected to the site page, via fetch. I get the CORS error.
Access to fetch at 'http://kladr-api.ru/api.php?query=%D0%9A%D1%80%D0%B0%D1%81&contentType=city&limit=2&withParent=0' from origin 'http://mysite.ru' has been blocked by CORS policy.
When accessing from node js I also get a response. Moreover, I have several domains and all of them have this error. I can get json everywhere, but not directly from the script connected to the matrix. Why can't I get data from an open api with a get request? What is the reason for this behavior?
2 answers
Cross-Origin Resource Sharing (CORS) read here
Use the official jQuery plugin KLADR: https://github.com/fias-api/jquery
$(function(){
$.kladr.api({
query: 'Крас',
type: 'city',
limit: 2,
withParent: 0
}, function(r){ console.log(r);})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/fias-api/[email protected]/jquery.kladr.min.js"></script>