如何使用traccar api获取设备

时间:2016-07-30 12:15:32

标签: javascript api swagger

我已经在谷歌和许多其他页面上做过研究,我可以通过traccar api here提取数据,所以我接下来研究如何使用它,最后通过mvalverde找到这个答案here 。在那篇文章中,他写了一些代码,如下:

// 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个问题,如何发布数据?我试着这样:

<!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不支持的媒体类型。我在代码中想念的是什么?

== CLOSED ==
下一个问题的最终代码在这里,

<!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>
谢谢,我解决了我的问题:)

2 个答案:

答案 0 :(得分:2)

未定义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);
    }
});

要发布数据,您可以发送如下的发布请求:

var data = {
    name: 'Test',
    ...
}

$.ajax({
    type: 'post',
    data: JSON.stringify(data),
    contentType: 'application/json',
    ...
});

答案 1 :(得分:0)

我在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.

您是否尝试过这样做?