如何使用traccar api获取设备
How to use traccar api to get devices
我已经对 google 和许多其他页面进行了研究,我可以通过 mvalverde 的 traccar api here so i do next research how to use it, and finally find this answer here 提取数据。在那post,他写了一些代码,像这样:
// find All devices - GET request
var result = HTTP.call(
"GET",
'http://your_domain:8082/api/devices/',
{
auth: 'email@xyz.com:password' ,
params: {
}
}
);
// add device
var name = 'name your device';
var uniqueId = '122323223232'; // usually this is the device imei
var result = HTTP.post(
'http://your_domain:8082/api/devices/',
{
auth: 'email@xyz.com:password' ,
data:{
groupId:null,
id: null,
lastUpdate:null,
status:'',
name:name,
uniqueId:uniqueId
}
}
);
他说是javascript,所以我尝试用javascript方式实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script type="text/javascript">
var result = HTTP.call(
"GET",
'http://103.23.20.176:8082/api/devices/',
{
auth: 'email:password' ,
params: {
}
}
);
console.log(result);
</script>
</body>
</html>
但我在控制台中收到错误消息。我怀疑它不是纯 javascript,如果是的话,你能指导我使用 javascript 编写实现吗?所以任何人都可以帮我解决这个问题。
ps : 我在js领域还是菜鸟
== 更新 ==
感谢 anton tananaev,我解决了这个问题,像这样:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
$.ajax({
type: 'get',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("admin:admin")
},
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
又是1个问题,如何post数据?我这样尝试:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
var name = "Bus 2";
var uid = "1212321233";
$.ajax({
type: 'post',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("email:pass")
},
data:{
id:null,
name:name,
uniqueId:uid,
groupId:null,
status:'',
lastUpdate:null
},
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
但我收到状态代码 415 不支持的媒体类型。我在代码中遗漏了什么?
== 关闭 ==
下一个问题的最终代码在这里,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
var name = "Bus 3";
var uid = "1243232133";
$.ajax({
type: 'post',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("email:pass")
},
contentType:"application/json",
data:JSON.stringify({
name:name,
uniqueId:uid
}),
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
谢谢,我的问题解决了:)
HTTP
对象未定义。您可以使用 jQuery。先添加库:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
然后进行 API 调用。像这样:
$.ajax({
type: 'get',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},
success: function (response) {
console.log(response);
}
});
您可以向 post 数据发送一个 post 请求,如下所示:
var data = {
name: 'Test',
...
}
$.ajax({
type: 'post',
data: JSON.stringify(data),
contentType: 'application/json',
...
});
我在 CORS 尝试通过本地主机或任何其他主机使用 API 时遇到问题:
Failed to load https://xxx.xxx.com.br/api/devices/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5000' is therefore not allowed access.
jquery.min.js:2 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://xxx.xxx.com.br/api/devices/ with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
你试过这样做吗?
我已经对 google 和许多其他页面进行了研究,我可以通过 mvalverde 的 traccar api here so i do next research how to use it, and finally find this answer here 提取数据。在那post,他写了一些代码,像这样:
// find All devices - GET request
var result = HTTP.call(
"GET",
'http://your_domain:8082/api/devices/',
{
auth: 'email@xyz.com:password' ,
params: {
}
}
);
// add device
var name = 'name your device';
var uniqueId = '122323223232'; // usually this is the device imei
var result = HTTP.post(
'http://your_domain:8082/api/devices/',
{
auth: 'email@xyz.com:password' ,
data:{
groupId:null,
id: null,
lastUpdate:null,
status:'',
name:name,
uniqueId:uniqueId
}
}
);
他说是javascript,所以我尝试用javascript方式实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script type="text/javascript">
var result = HTTP.call(
"GET",
'http://103.23.20.176:8082/api/devices/',
{
auth: 'email:password' ,
params: {
}
}
);
console.log(result);
</script>
</body>
</html>
但我在控制台中收到错误消息。我怀疑它不是纯 javascript,如果是的话,你能指导我使用 javascript 编写实现吗?所以任何人都可以帮我解决这个问题。
ps : 我在js领域还是菜鸟
== 更新 ==
感谢 anton tananaev,我解决了这个问题,像这样:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
$.ajax({
type: 'get',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("admin:admin")
},
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
又是1个问题,如何post数据?我这样尝试:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
var name = "Bus 2";
var uid = "1212321233";
$.ajax({
type: 'post',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("email:pass")
},
data:{
id:null,
name:name,
uniqueId:uid,
groupId:null,
status:'',
lastUpdate:null
},
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
但我收到状态代码 415 不支持的媒体类型。我在代码中遗漏了什么?
== 关闭 ==
下一个问题的最终代码在这里,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>try traccar api</title>
</head>
<body>
<script src="js/jquery-2.2.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
var name = "Bus 3";
var uid = "1243232133";
$.ajax({
type: 'post',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa("email:pass")
},
contentType:"application/json",
data:JSON.stringify({
name:name,
uniqueId:uid
}),
success: function (response) {
console.log(response);
}
});
</script>
</body>
</html>
谢谢,我的问题解决了:)
HTTP
对象未定义。您可以使用 jQuery。先添加库:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
然后进行 API 调用。像这样:
$.ajax({
type: 'get',
url: 'http://103.23.20.176:8082/api/devices/',
headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},
success: function (response) {
console.log(response);
}
});
您可以向 post 数据发送一个 post 请求,如下所示:
var data = {
name: 'Test',
...
}
$.ajax({
type: 'post',
data: JSON.stringify(data),
contentType: 'application/json',
...
});
我在 CORS 尝试通过本地主机或任何其他主机使用 API 时遇到问题:
Failed to load https://xxx.xxx.com.br/api/devices/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5000' is therefore not allowed access.
jquery.min.js:2 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://xxx.xxx.com.br/api/devices/ with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
你试过这样做吗?