无法加载资源:服务器以 404 状态响应 PurplAir

问题描述 投票:0回答:1

不要喷我,但我正在尝试使用 PHP 创建 PurplAir API 请求,当我运行代码时,找不到结果。 我附上了我的代码、存储库和本地服务器的屏幕截图。

Result

Repo

我尝试观看视频,并解决其他堆栈问题。


更新


这是我的代码:

<html>
    <body>
        <script type="text/javascript">
        function makeAPIRequest() {
            var request = new XMLHttpRequest();
            var request.open('GET', https: '//api.purpleair.com/v1/sensors/142696');
            var request.setRequestHeader('X-API-Key', 19E223A3-3837-11ED-B5AA-42010A800006);
            var request.send();

            $request.onload = ()=>{
                const given = JSON.parse(request.response);
                console.log(given.sensor);

                //Get PM2.5 from json
                var PM = given.sensor.stats["pm2.5_10minute"];
                console.log("PM2.5: " + PM);

                //calculate AQI
                calcAQIFromPM(PM);
                console.log("AQI: " + AQI);
            }
        }

        //Calculates AQI value (using formula from link below)
        //  - https://metone.com/how-to-calculate-aqi-and-nowcast-indices/
        //TODO: Fix this formula for ALT cf=3 (Purple Air's default)
        //  - https://www.sciencedirect.com/science/article/abs/pii/S135223102100251X?via%3Dihub
        function calcAQIFromPM(pm) {
            //DO NOT CHANGE TABLE
            const table = [
                [0.0, 12.0, 0, 50],
                [12.1, 35.4, 51, 100],
                [35.5, 55.4, 101, 150],
                [55.5, 150.4, 151, 200],
                [150.5, 250.4, 201, 300],
                [250.5, 500.4, 301, 500],
            ];

            //formula for calc AQI
            const computeAqi = (concI, [concLo, concHi, aqiLo, aqiHi]) =>
                Math.round(
                ((concI - concLo) / (concHi - concLo)) * (aqiHi - aqiLo) + aqiLo
                );

            //find table values where (pm > low && pm <= high)
            const values = table.find(([concLo, concHi, aqiLo, aqiHi]) => (pm >= concLo && pm <= concHi));

            //Set AQI (max 500)
            AQI = values ? computeAqi(pm, values) : 500;
        }

        //------STYLE SECTION------
        function setAQIText() {
            document.getElementById("AQI").textContent = AQI;
        }

        //TODO: fix colors to match PurpleAir
        function setAQIColor() {
            var color = document.getElementById("colorDiv")

            if( AQI < 50 ) {
                color.style.backgroundColor = "green";
            } else if( AQI >= 50 && AQI < 100 ) {
                color.style.backgroundColor = "yellow";
            } else if( AQI >= 100 && AQI < 150 ) {
                color.style.backgroundColor = "orange";
            } else if( AQI >= 150 && AQI < 200 ) {
                color.style.backgroundColor = "red";
            } else if( AQI >= 200 && AQI < 300 ) {
                color.style.backgroundColor = "purple";
            } else if( AQI >= 300 ) {
                color.style.backgroundColor = "brown";
            }
        }
        </script>
            <h1> Current AQI </h1>
            <div id="colorDiv" height="20px" width="20px">
                <h2 id="AQI">---</h2>
            </div>
            <button id="getAQI">Get Current AQI</button>
    </body>
</html>

<?php
?>
php html webserver http-status-code-404
1个回答
0
投票

在代码的第 7 行 (

setRequestHeader(
...) 在 API 密钥两边添加引号,它是一个字符串。

还有,

var request.open('GET', https: '//api.purpleair.com/v1/sensors/142696');

应该是

var request.open('GET', 'https://api.purpleair.com/v1/sensors/142696');

我的意思是再次检查你的代码并检查所有转义的字符串,你知道如何用 PHP 编码吗?这些看起来像是一些非常基本的 PHP 错误,与 PurpleAir 无关。

一旦你解决了这些问题,我怀疑你可能会遇到 CORS 问题。

当您向我们展示您的控制台时,转到网络选项卡,尝试找到

https://api.purpleair.com/v1/sensors/142696
的请求,单击该请求,然后检查标头设置是否正确,然后转到响应选项卡并查看它返回的内容,这可能很有趣.

© www.soinside.com 2019 - 2024. All rights reserved.