This article mainly introduces how to dynamically create a script tag to implement cross-origin resource access. If you need it, you can refer to it and hope to help you.
Login.html
The Code is as follows:
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 ("Login successful ");
}
Else
{
Alert ("Logon Failed ");
}
}
Script
User name:
Password:
Login. jsp
The Code is as follows:
<% @ 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 ");
<%
}
%>