如何使用VisualStudio2019和javascript从api获取数据?
我想从api中获取一些数据,并用java脚本显示在警报框中,但我有一个错误,无法显示数据 错误是: 我的代码:如何使用VisualStudio2019和javascript从api获取数据?,javascript,c#,node.js,api,web,Javascript,C#,Node.js,Api,Web,我想从api中获取一些数据,并用java脚本显示在警报框中,但我有一个错误,无法显示数据 错误是: 我的代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="X-UA-Compatible"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src https://*; child-src 'none';">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
</head>
<body>
<script defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCLLN5Uon6l2A41J2N0whRM7WQ9YAmRFeQ&callback=initMap">
</script>
<script type="text/javascript">
var markers = [
{ "title": 'Населено място', "lat": '41.19197', "lng": '25.33719', "description": 'Информация за населеното място' }
];
window.onload = function () {
LoadMap();
DrawChart();
}
function LoadMap() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 7,
minZoom: 7.5,
maxZoom: 10,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var infoWindow = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (var i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title,
icon: 'http://192.168.0.1/markers/marker.png',
opacity: 0.4
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent("<div style = 'width:300px;min-height:100px'>" + data.description + "</div>");
infoWindow.open(map, marker)
});
})(marker, data);
latlngbounds.extend(marker.position);
}
var bounds = new google.maps.LatLngBounds();
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
}
function async getData() {
let url = `http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8`;
let response = await fetch(url);
if (response.ok) {
let json = await response.json();
window.alert(json);
} else {
alert("HTTP-Error: " + response.status);
}
}
}
</script>
<div id="dvMap" style="width: 100%; height: 600px">
</div>
<div style="width: 100%; height: 270px">
<canvas id="mixed-chart" width="1000" height="270"></canvas>
</div>
</body>
</html>
我可以举一些例子来说明如何解决这个问题吗
我尝试了不同的方法从API获取数据,但每个方法的错误都是一样的
这是一系列警告
让url=`http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8`代码>
可能不是抛出实际错误的地方,我假设是
let response=wait-fetch(url);
由于没有将getData
函数标记为async,因此不能在getData
函数中使用wait
异步函数getData(){
让url=`http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8`;
let response=等待获取(url);
if(response.ok){
让json=wait response.json();
window.alert(json);
}否则{
警报(“HTTP错误:+response.status”);
}
}
或者使用。然后
函数getData(){
让url=`http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8`;
获取(url)。然后(res=>{
如果(res.ok){
让json=wait response.json();
window.alert(json);
}否则{
警报(“HTTP错误:+response.status”);
}
})
}
试着用“”而不是。当您在URL上输入来自以下对象的值时,请使用
:const URL=http://192.168.0.1:3000/sdfasdfadf/${data.id}
试试这个:constURL=”http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8“Jonas Grønbek我尝试了您的两种方法,但错误是相同的…URL来自您构建的API?将async置于函数之前,并尝试使用“”将URL置于获取上
let url = `http://192.168.0.1:3000/?date=2019-10-20&id=1010&daysForward=8`;