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

2019-12-23,,

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>

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");
 <%
}
%>

您可能感兴趣的文章:

  • JavaScript动态添加css样式和script标签
  • IE8中动态创建script标签onload无效的解决方法
  • Script标签与访问HTML页面详解
  • javascript标签在页面中的位置探讨
  • script标签属性type与language使用选择
  • script标签的 charset 属性使用说明
  • javascript 获取url参数和script标签中获取url参数函数代码
  • asp.net(C#) 动态添加非ASP的标准html控件(如添加Script标签)
  • 有趣的script标签用getAttribute方法来自脚本吧
  • 浅谈js script标签中的预解析

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

下载本文的Word格式文档,以方便收藏与打印。