lettura JSON

10
Nov
2019
 

E' Possibile controllare un rel tramite il browser escludendo le limitazioni imposte dal CrossDomain ed utilizzando JsonP. Per fare questo occorre creare un file html per la lettura, tipo questo di seguito :

<!DOCTYPE html>
<html>
<head>
<title>New Page 12</title>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css"/>
 
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>if (!window.jQuery){ document.write('<script src="http://www.miosalento.it/js/jquery-1.11.1.min.js"><\/script>'); }</script>    

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
 
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<style>
</style>
<script>
       function cmd(arg) {  
            url = "http://192.168.1.101/"+arg
            //alert(url)
            $("#console").html($("#console").html()+"<BR>Requesting..."+url);
 
            $.get(url, function( data ) {
                $("#console").html($("#console").html()+"<BR>Response<pre>"+data.ledstate+"</pre>");
              });
        }


$(function(){

$(".chiedi_1,.chiedi_2").bind("click", function () {
	var azione = $(this).data("azione")
	var url="http://192.168.1.101/"+azione+"?dati=?"

$.ajax({
    url: url,
    type: "get",
	async: true,
    crossDomain: true,
    dataType: 'jsonp',
    jsonp: false,
   	jsonpCallback: "dati", 
    method: "GET",
   	contentType: "application/json",
    headers: {
              "accept": "application/json",
              "Access-Control-Allow-Origin":"*"
          }     
})
.done(function(data) {
         alert(data.ledstate);
})
    .fail(function(jqXHR, textStatus, errorThrown) {
        alert("error " + textStatus);
        alert("incoming Text " + jqXHR.responseText);
        alert(errorThrown);
});
}).css("cursor","pointer");	 


 

})
</script>
</head>

<body>
<div class="container">

    <button onclick="return cmd('ledon')" form="form1" value="Submit">LED ON</button>
    <button onclick="return cmd('ledoff')" form="form1" value="Submit">LED OFF</button>
    <button onclick="return cmd('info')" form="form1" value="Submit">Info</button>
 <br><br>
    <div id="console" name="console" width=200 height=300>Ready.</div>
 
<div class="chiedi_1" data-azione="ledon">led on</div>   
<div class="chiedi_2" data-azione="ledoff">led off</div>   
<div class="chiedi_3" data-azione="info">info</div>   
 
  
</div>
</body>
</html>