2016-07-30 78 views
2

我已經完成了谷歌和許多其他頁面的研究,我可以通過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> 

謝謝你,我解決我的問題:)

+0

什麼錯誤,你所看到的? – RiggsFolly

+0

ReferenceError:HTTP未定義@RiggsFolly – dyaadin

回答

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); 
    } 
}); 

要發佈的數據,你可以發送POST請求是這樣的:

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

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

謝謝@anton,但是,如何添加數據? – dyaadin

+0

增加了發佈數據的信息。 –

+0

謝謝:)我很感激在這裏遇到你@anton – dyaadin

相關問題