2017-08-01 82 views
-1

我想獲得'http://localhost:8080/api/printer?exclude=temperature'[HTTP/1.1]提供的Json數據。如何從Restful API URL獲取JSON數據?

這是要提供的Json數據。

{ 
    "state": { 
    "text": "Operational", 
    "flags": { 
     "operational": true, 
     "paused": false, 
     "printing": false, 
     "sdReady": true, 
     "error": false, 
     "ready": true, 
     "closedOrError": false 
    } 
    } 
} 

的類型是 'GET' 和Content-Type爲 '應用/ JSON的'

而且下面是我的JSP整個源代碼。

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
<script type="text/javascript" language="javascript"> 
function print_stat(){ 
    jQuery.ajax({ 
     type: 'GET', 
     async: false , 
     url: 'http://localhost:8080/api/printer?exclude=temperature&apikey=1234567...', 
     contentType: 'application/json; charset=utf-8', 
     success: function (data) { 
     alert("TEST"); 
     }, 
     error : function(x, e) { 
      alert('server error occoured'); 
      if(x.status==0){ alert('0 error'); 
      }else if(x.status==404){ alert('404 error'); 
      }else if(x.status==500){ alert('500 error'); 
      }else if(e=='parsererror'){ alert('Error.nParsing JSON Request failed.'); 
      }else if(e=='timeout'){ alert('Time out.'); 
      }else { alert(x.responseText); } 
     } 
    }); 
} 
</script> 
<body> 
<input type="button" onclick="print_stat()" value="test"> 
</body> 

我看不到「TEST」彈出式窗口。 沒有任何反應。

我該如何獲取數據?

請幫幫我。

+4

'異步:假的, '< - 主意不好 – epascarello

+0

因此開發者控制檯中沒有錯誤? – epascarello

+0

你包括jquery嗎?聽起來不像。 –

回答

0

你可以試試這個,

語法:

$.ajax({ 
    beforeSend: function(xhrObj){ 
     xhrObj.setRequestHeader("Content-Type","application/json"); 
     xhrObj.setRequestHeader("Accept","application/json"); 
    }, 
    type: "POST", 
    url: uri,  
    data: jsonStrJson,    
    dataType: "json", 
    success: function(json){ 
     console.log(json); 
    } 
}); 

你的代碼應該是這樣的100%的工作和測試代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width"> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 
</head> 
<body> 
<script type="text/javascript" language="javascript"> 
function print_stat(){ 
    jQuery.ajax({ 
     type: 'GET', 
     async: false , 
     url: 'http://localhost:8080/api/printer?exclude=temperature&apikey=1234567...', 
     dataType: 'json', 
     success: function (data) { 
     alert("TEST"); 
     console.log(data); 
     }, 
     error : function(x, e) { 
      alert('server error occoured'); 
      if(x.status==0){ alert('0 error'); 
      }else if(x.status==404){ alert('404 error'); 
      }else if(x.status==500){ alert('500 error'); 
      }else if(e=='parsererror'){ alert('Error.nParsing JSON Request failed.'); 
      }else if(e=='timeout'){ alert('Time out.'); 
      }else { alert(x.responseText); } 
     } 
    }); 
} 
</script> 
<body> 
<input type="button" onclick="print_stat()" value="test"> 
</body> 
</body> 
</html> 
+0

在GET上沒有意義設置contentType標頭...沒有請求內容 – charlietfl