2017-04-21 65 views
0

我正在做這個API調用,並沒有得到任何結果。當我通過瀏覽器撥打電話並使用該imo進行搜索時,我可以找回船隻信息,但不能通過代碼調用。打電話給Api海事交通javascript

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
    <div id="pr"></div> 
    <script> 

    getInfoVessel("9146314"); 


    function httpGetAsync(url, callback) { 
     var xmlHttp = new XMLHttpRequest(); 
     xmlHttp.onreadystatechange = function() { 
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
       callback(xmlHttp.responseText); 
      }  
     } 
     xmlHttp.open("GET", url, true); 
     xmlHttp.send(null); 
    } 

    function getInfoVessel(IMO){ 
     httpGetAsync('http://services.marinetraffic.com/api/exportvessel/v:5/7[herecomesthekey]/protocol:jsono/imo:9146314', function(response) { 
      document.getElementById('pr').innerHTML = response; 
     }); 
    } 
    </script> 
    </body> 
</html> 

回答

0

的反應是良好的,但Status Code:200 OK有一個錯誤在響應KEY NOT FOUND

試試這個代碼:

function httpGetAsync(url, callback) { 
    var xmlHttp = new XMLHttpRequest(); 
    xmlHttp.open("GET", url, true); 
    xmlHttp.send(null); 
    xmlHttp.onreadystatechange = function() { 
     if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
      callback(xmlHttp.responseText); 
     } 
    } 
} 
function getInfoVessel(IMO){ 
    httpGetAsync('http://services.marinetraffic.com/api/exportvessel/v:5/7[herecomesthekey]/protocol:jsono/imo:' + IMO, function(response) { 
     console.log(response) 
     document.getElementById('pr').innerHTML = response; 
    }); 
} 

getInfoVessel("9146314");