如何使用JavaScript / jQuery在SensorThings API中POST / GET / PATCH / DELETE

时间:2018-03-06 04:17:35

标签: javascript jquery sensorthings

我正按照http://developers.sensorup.com/docs/中的说明在SensorThings API中尝试POST / GET / PATCH / DELETE。我可以成功地使用HTTP(使用Postman)和cURL(使用bash shell)进行实验。但是,我无法尝试使用JavaScript / jQuery。我对这两种脚本语言都没有深入的经验。我试图制作这样一个页面。

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>SensorThings API Test</title>
</head>
<body>
    <script>
        // I followed the following lines from the http://developers.sensorup.com/docs/ page
        var json = JSON.stringify({
            "name": "Temperature Monitoring System",
            "description": "Sensor system monitoring area temperature",
            "properties": {
                "Deployment Condition": "Deployed in a third floor balcony",
                "Case Used": "Radiation shield"
            }
        }
        });

        $.ajax({
            url: "https://scratchpad.sensorup.com/OGCSensorThings/v1.0/Things",
            type: "POST",
            data: json,
            contentType: "application/json; charset=utf-8",
            success: function(data){
                console.log(data);  
            },
            error: function(response, status){
                console.log(response);
                console.log(status);
            }
        });
    </script>
</body>
</html>

我在浏览器中运行它,但控制台显示错误消息((参数列表后面的(未捕获的SyntaxError:缺失)))。我想也许我应该在标签之前插入一些JavaScript代码,但我不知道该怎么做。有人可以提供一个页面,可以让我&#34; POST&#34;进入服务器的东西?

提前致谢...

1 个答案:

答案 0 :(得分:1)

您的json数据格式不正确,会引发错误。您已输入额外的&#39;}&#39;到了json 正确的格式如下所示

var json = JSON.stringify({
    "name": "Temperature Monitoring System",
    "description": "Sensor system monitoring area temperature",
    "properties": {
        "Deployment Condition": "Deployed in a third floor balcony",
        "Case Used": "Radiation shield"
    }            
});

而且你还没有添加jquery libratry。如果它没有在代码中显示,请忘记我说的内容。首先添加jquery库,然后添加实际代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

&#13;
&#13;
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>SensorThings API Test</title>
</head>
<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        // I followed the following lines from the http://developers.sensorup.com/docs/ page
        var json = JSON.stringify({
            "name": "Temperature Monitoring System",
            "description": "Sensor system monitoring area temperature",
            "properties": {
                "Deployment Condition": "Deployed in a third floor balcony",
                "Case Used": "Radiation shield"
            }
        
        });

        $.ajax({
            url: "https://scratchpad.sensorup.com/OGCSensorThings/v1.0/Things",
            type: "POST",
            data: json,
            contentType: "application/json; charset=utf-8",
            success: function(data){
                console.log(data);  
            },
            error: function(response, status){
                console.log(response);
                console.log(status);
            }
        });
    </script>
</body>
</html>
&#13;
&#13;
&#13;