The method of dynamically creating script tags to achieve cross domain resource access is introduced

  • 2020-03-30 02:08:05
  • OfStack

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(" Log in successfully ");
   }
   else
   {
        alert(" Logon failure ");
   }
}
</script>
<body>
 User name: <input name="name" id="name" type="text" />
 Password: <input name="password" id="password" type="password" />
<input name="" value="login" type="button" onclick="userLogin();" />
</body>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");
if (name.equals("admin") && password.equals("admin"))
{
 request.getSession().setAttribute("admin","admin");
 %>
 myloginState("0"); 
 <%
}
else
{
 %>
  myloginState("1"); 
 <% 
}
%>


Related articles: