我想从天气API接收数据。 他们说他们使用json API。但是当我查看请求网址时,它是
https://frost.met.no/sources/ v0.jsonld ?types = SensorSystem
我尝试使用标准js脚本,但是使用此url,我得到了错误的数据。 我已经挣扎了好几个星期了!
有人给我小费吗?
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<div id="ip"></div>
<div>
<div id="response">
</div>
<p id="demo"></p>
<input type="button" class="btn btn-primary" value="Call Web API" onclick="javascript:CallWebAPI();" /><br>
<input type="button" class="btn btn-primary" value="Call fetchStatus" onclick="javascript:fetchStatus();" /><br>
<div id="mydiv" style="color:red"></div>
<script type="text/javascript">
var myIpAdresse;
//Hent datamskinens IP adressen
getIP()
//************************************************************************************************************
//************************************************************************************************************
//** variabler
var url = 'https://frost.met.no/sources/v0.jsonld?types=SensorSystem&geometry=nearest(POINT(10.905166779011005%2060.060156717165675))';
var x;
var txt;
var MyUserName = '0c23a1ce-1d3b-4481-9c0a-ecc505e39620';
//var credentials= window.btoa(MyUserName + ':' + MySecret);
var credentials= window.btoa(MyUserName + ':');
var logData = makeBaseAuth(MyUserName, MySecret);
const httpGetOptions ={withCredentials: true,};
//alert("Authorization: Basic " + credentials);
//alert(credentials); //dXNlcm5hbWU6cGFzc3dvcmQ=
//************************************************************************************************************
//************************************************************************************************************
//** Hent IP Adressen
function getIP() {
var MyIP = new XMLHttpRequest();
MyIP.open('GET', 'https://api.ipify.org?format=json', true);
MyIP.onload=function() {
// process response
if (MyIP.status == 200) {
// parse JSON data
var myObjIP = JSON.parse(MyIP.responseText);
myIpAdresse = myObjIP.ip;
document.getElementById('ip').innerHTML = myIpAdresse;
} else {
alert('Error!');
}
};
MyIP.send();
}
//************************************************************************************************************
//************************************************************************************************************
//************************************************************************************************************
//************************************************************************************************************
//** Hent data fra URL
function CallWebAPI() {
//var client = null;
var url2 = new URL('/sources/v0.jsonld?types=SensorSystem', 'https://frost.met.no');
url2=url2.toString();
var url ='https://frost.met.no/sources/v0.jsonld?types=SensorSystem';
var client = new XMLHttpRequest()
client.open('GET', url, true)
client.onload=function() {
// process response
if (client.status == 200) {
// parse JSON data
alert(JSON.parse(client.responseText));
} else {
alert('Error!');
}
};
client.setRequestHeader('Access-Control-request-Methods', 'GET');
client.setRequestHeader('Content-Type', 'application/vnd.api+json');
client.setRequestHeader('Authorization','Basic ' + credentials);
client.send(null);
client.onerror = function() {
alert('xmlHTTP Error', client.responseText)
}
}
function fetchStatus() {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if(this.readyState == 4)
returnStatus(this.status);
}
client.open('HEAD', url1);
client.setRequestHeader('Accept','application/json');
client.setRequestHeader('Authorization','Basic ' + credentials);
client.send();
}
function makeBaseAuth(user,password) {
var tok = user + ':' + password;
var hash = btoa(tok);
//alert(hash);
return 'Basic ' + hash;
}
</script>
</body>
</html>
答案 1 :(得分:1)