动态创建script标签实现跨域资源访问的方法介绍

前端技术 2023/09/06 JavaScript

login.html

复制代码 代码如下:

<script>

function request(id,url){
     oScript = document.getElementById(id);
     var head = document.getElementsByTagName(\"head\").item(0);
     if (oScript) {
        head.removeChild(oScript);
     }
     oScript = document.createElement(\"script\");
     oScript.setAttribute(\"src\", url);
     oScript.setAttribute(\"id\",id);
     oScript.setAttribute(\"type\",\"text/javascript\");
     oScript.setAttribute(\"language\",\"javascript\");
     head.appendChild(oScript);
     return oScript;
}

 

function userLogin(){
    var username=document.getElementById(\'name\').value;
    var password=document.getElementById(\'password\').value;

    //alert(username+\"--\"+password);

 var url=\'http://127.0.0.1:8080/EasyCMS/login.jsp?name=\'+encodeURI(username)+\'&password=\'+encodeURI(password)+\'&s=\' + (new Date()).getTime();

 //alert(\"url=\"+url);
    var login=request(\"loginScript\",url);
}

function myloginState(state){

      alert(\"ret:\"+state);
      if (state==0)
   {
  alert(\"登陆成功\");
   }
   else
   {
        alert(\"登陆失败\");
   }

}

</script>
<body>
用户名:<input name=\"name\" id=\"name\" type=\"text\" />
密码:<input name=\"password\" id=\"password\" type=\"password\" />
<input name=\"\" value=\"login\" type=\"button\" onclick=\"userLogin();\" />
</body>

本文地址:https://www.stayed.cn/item/16947

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。