我在google和许多其他页面上做过研究,我可以通过traccar 这里提取数据,所以接下来我会研究如何使用它,最后通过mvalverde找到这个答案这里。在那篇文章里,他写了一些代码,比如:
// 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领域的新秀。
==更新==
感谢安东·塔纳纳耶夫,我解决了这个问题,就像这样:
<!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>还有一个问题,如何发布数据?我试着这样做:
<!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>谢谢,我解决了我的问题
发布于 2016-07-30 13:34:46
未定义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请求:
var data = {
name: 'Test',
...
}
$.ajax({
type: 'post',
data: JSON.stringify(data),
contentType: 'application/json',
...
});发布于 2018-09-25 20:56:52
在CORS试图通过localhost或任何其他主机使用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.你试过做这种事吗?
https://stackoverflow.com/questions/38673851
复制相似问题