【问题标题】:json with request format jsonldjson 请求格式为 jsonld
【发布时间】:2019-12-18 04:45:27
【问题描述】:

我想从天气 API 接收数据。 他们说他们使用 json API。但是当我查看请求 URL 时,它是

https://frost.met.no/sources/v0.jsonld?types=SensorSystem

我尝试使用标准的 js 脚本,但是使用这个 url,我得到了错误的数据。 我已经挣扎了好几个星期了!

有人给我小费吗?

【问题讨论】:

  • 请分享你的js脚本。

标签: json json-ld


【解决方案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>

【讨论】:

    【解决方案2】:

    当您从服务器获得答案时,您的client.responseText 中有什么内容?与“普通” JSON 的唯一区别 - 以 @symbol 开头的标签。因此,如果您的client.responseText 如下所示,那么您也应该能够解析 JSON-LD(您可以找到解析 here 的简单示例)。

    我使用您的访问数据来获取 JSON-LD。然后我将它保存在变量myJSONLDvar myObj = JSON.parse(myJSONLD) 中。请通过工作示例检查此link

    【讨论】:

    • 嗨。我得到一个 404 错误。所以我真的没有得到答案。我用 GET 发送,但是当我在 chrome 中使用检查时,它说选项左右。有一个奇怪的故障!
    • 嗨。这是 CORS 问题 :( 我不知道如何通过这个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2013-01-17
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多