Advertisement
piffy

Esempio JSONP

Aug 18th, 2022 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.10 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="it">
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>Jsonp demo</title>
  6.     <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  7.     <script>
  8.         function jsonp(){
  9.             var now = new Date();
  10.             url = "http://webservice.helmutkarger.de/php/jsonp.php?time="
  11.                 +now.getTime()+"&callback=callback";
  12.             var script = document.createElement("script");
  13.             script.setAttribute("src", url);
  14.             script.setAttribute("type", "text/javascript");
  15.             document.getElementsByTagName("head")[0].appendChild(script);
  16.         }
  17.  
  18.         function callback(data) {
  19.             document.getElementById("risposta_jsonp").innerHTML = data;
  20.         }
  21.  
  22.         function jQueryJSONCORS() {
  23.             $("#risposta_jsonp").html("Controllare l'output della console");
  24.             var now = new Date();
  25.             $.getJSON("http://webservice.helmutkarger.de/php/jsonp.php?time="
  26.                 +now.getTime()+"&callback=callback",function(data){
  27.                $("#risposta_jsonp").html("data");
  28.             });
  29.         }
  30.         function jQueryJSON() {
  31.             var now = new Date();
  32.             $.ajax({
  33.                 url: "http://webservice.helmutkarger.de/php/jsonp.php?",
  34.                 dataType: "jsonp",
  35.                 jsonpCallback: "callback"
  36.             })
  37.  
  38.  
  39.         }
  40.     </script>
  41. </head>
  42.  
  43. <body>
  44. <h1>JasonP Demo</h1>
  45.  
  46. <div>
  47.     <h3>Uso di JSONP</h3>
  48.     <div>
  49.         <p>Chiamata JSONP da un server <b>'ignoto'</b></p>
  50.         <input id="pulsante1" type="button" value="Richiesta di indirizzo IP (JS)"
  51.               onclick="jsonp()" />
  52.         <input id="pulsante2" type="button" value="Richiesta di indirizzo IP (CORS PROB)"
  53.               onclick="jQueryJSONCORS()" />
  54.         <input id="pulsante3" type="button" value="Richiesta di indirizzo IP (jQuery)"
  55.               onclick="jQueryJSON()" />
  56.         <p id="risposta_jsonp" style="border: solid 1px darkred;">Risposta del server:</p>
  57.     </div>
  58.     <h3>Nota: ricaricare la pagina ad ogni tentativo</h3>
  59. </div>
  60.  
  61.  
  62. </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement