我已經完成了谷歌和許多其他頁面的研究,我可以通過traccar api提取數據here所以我做下一個研究如何使用它,並最終找到這個答案here mvalverde 。在那個崗位上,他寫一些代碼,就像這樣:如何使用traccar api獲取設備
// find All devices - GET request
var result = HTTP.call(
"GET",
'http://your_domain:8082/api/devices/',
{
auth: '[email protected]: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 protected]: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領域的新秀
== == UPDATE
感謝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個問題,怎麼樣發佈數據?我試試這樣:
<!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>
謝謝你,我解決我的問題:)
什麼錯誤,你所看到的? – RiggsFolly
ReferenceError:HTTP未定義@RiggsFolly – dyaadin