piffy icon

Esempio JSONP

piffy | PRO | 08/18/22 05:40:15 PM UTC (Edited) | 0 ⭐ | 1091 👁️ | Never ⏰ | []
HTML |

2.1 KB

|

None

|

0 👍

/

0 👎

<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="utf-8">
    <title>Jsonp demo</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
        function jsonp(){
            var now = new Date();
            url = "http://webservice.helmutkarger.de/php/jsonp.php?time="
                +now.getTime()+"&callback=callback";
            var script = document.createElement("script");
            script.setAttribute("src", url);
            script.setAttribute("type", "text/javascript");
            document.getElementsByTagName("head")[0].appendChild(script);
        }
 
        function callback(data) {
            document.getElementById("risposta_jsonp").innerHTML = data;
        }
 
        function jQueryJSONCORS() {
            $("#risposta_jsonp").html("Controllare l'output della console");
            var now = new Date();
            $.getJSON("http://webservice.helmutkarger.de/php/jsonp.php?time="
                +now.getTime()+"&callback=callback",function(data){
                $("#risposta_jsonp").html("data");
            });
        }
        function jQueryJSON() {
            var now = new Date();
            $.ajax({
                url: "http://webservice.helmutkarger.de/php/jsonp.php?",
                dataType: "jsonp",
                jsonpCallback: "callback"
            })
 
 
        }
    </script>
</head>
 
<body>
<h1>JasonP Demo</h1>
 
<div>
    <h3>Uso di JSONP</h3>
    <div>
        <p>Chiamata JSONP da un server <b>'ignoto'</b></p>
        <input id="pulsante1" type="button" value="Richiesta di indirizzo IP (JS)"
               onclick="jsonp()" />
        <input id="pulsante2" type="button" value="Richiesta di indirizzo IP (CORS PROB)"
               onclick="jQueryJSONCORS()" />
        <input id="pulsante3" type="button" value="Richiesta di indirizzo IP (jQuery)"
               onclick="jQueryJSON()" />
        <p id="risposta_jsonp" style="border: solid 1px darkred;">Risposta del server:</p>
    </div>
    <h3>Nota: ricaricare la pagina ad ogni tentativo</h3>
</div>
 
 
</body>
</html>

Comments