我正在尝试按照 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>
我在浏览器中运行它,但控制台显示错误消息((Uncaught SyntaxError: missing ) 在参数列表之后)。我想也许我应该在标签前插入一些 JavaScript 代码,但我不知道该怎么做。有人可以提供一个页面,让我“发布”一个东西到服务器吗?
提前致谢...
最佳答案
您的 json 数据格式无效,这会引发错误。您在 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 库。如果它没有在代码中显示,请忘记我说的。否则先添加 jquery 库,然后再添加实际代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!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>
关于javascript - 如何使用 JavaScript/jQuery 在 SensorThings API 中发布/获取/修补/删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123305/