Use jsonp to solve cross-domain problems. Open a service through Node and visit it once. Why does the server print it twice?

one of the cross-domain solutions: jsonp

// server.js
var qs = require("queryString");
var http = require("http");
var server = http.createServer();

server.on("request", function(req, res) {
  console.log("irene"); // irene

  var params = qs.parse(req.url.split("?")[1]);
  var fn = params.callback;

  // jsonp
  res.writeHead(200, { "Content-Type": "text/javascript" });
  res.write(fn + "(" + JSON.stringify(params) + ")");

  res.end();
})
server.listen("8094");
console.log("Server is running at port 8094...");

// jsonp.html
<html>
  <head></head>
  <body>
    <script>
      var script = document.createElement("script");
      script.type = "text/javascript";
  
      // onBack
      script.src = "http://www.domain2.com:8094/login?user=admin&callback=onBack";
      document.head.appendChild(script);
  
      // 
      function onBack(res) {
          alert(JSON.stringify(res));
      }
   </script>
  </body>
 </html>
 

Why does the "irene"" in server.js print twice when I visit jsonp.html, in my browser?

Mar.15,2021

is because two requests have been made. Once localhost:8094, and once localhost:8094/favicon.ico, print res.url, you can see it clearly


favicon.ico. Then you don't use a cross-domain environment at all, you just try jsonp.
favicon.ico is a website that will request it and initiate it automatically. Add your jsonp logic to the button click event and trigger it and you will see it only once.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4d49d-44f18.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4d49d-44f18.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?