百度跨域ajax

ajax.html

<html>
<head>
<script>
function ws_results(obj) {
alert(obj.ResultSet.totalResultsAvailable);
}

</script>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
var loginScript;
<!--
function G(id){if(typeof(id)=="string"){return document.getElementById(id);}return id;}
function showInfo(obj){
    
if(obj.checked == true){
        G(
"memInfo").style.display="block";
    }
else{
        G(
"memInfo").style.display="none";
    }
}
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;
}
var loginTimer=null;
var loginState=-1;
var tryTime=0;
function PSP_ik(isOk){
    
if(isOk==0){
        G(
"errorInfo").style.display="none";
        loginState
=1;
        
if(parent.loginSuccess){
            parent.Pop.hide();
            parent.loginSuccess();
        }
    }
    
else
    {
        loginFalse();
    }
}

function loginFalse(){
    loginState
=0;
    
var err=G("errorInfo");
    err.innerHTML
="用户名或密码错误,请重新登录";
    err.style.display
="block";
    G(
"username").focus();
    tryTime
++;
    
if(tryTime>1){
        onLoginFailed();
    }
}
function onLoginFailed(){
    
if(parent.onLoginFailed){
        parent.Pop.hide();
        parent.loginFailed();
    }
else{
        document.login.u.value
=escape("http://zhidao.baidu.com/q"+parent.location.search);
        doucment.login.submit();
    }
        
}
function loginTimeout(){

    
if(loginState==-1){
        
var err=G("errorInfo");
        err.innerHTML
="操作超时,请重新登录";
        err.style.display
="block";
        G(
"username").focus();
    }
}

function userLogin(){

    
var url = 'http://uia.tel.comsys.net.cn/1.jsp?s='+(new Date()).getTime()+'&appid=YahooDemo&query=Madonna&output=json&callback=ws_results' ;
    loginState
=-1;
    
var login=request("loginScript",url);
    loginScript
=login;
    login.onreadystatechange
=function(){
                    
var state = login.readyState; ;
                    
if (state == "loaded" || state == "interactive" || state == "complete") {    
                        alert(msg);
//msg与服务端返回的变量一致
                    } 
    }
    loginTimer 
= setTimeout(loginTimeout, 5000);

}
window.onload
=function(){
    
}
//-->
</SCRIPT>


<input onclick="userLogin()" type="button" value="userLogin"/>
</body>
</html>

1.jsp

<%@ page language="java" contentType="text/html; charset=gbk" %>
var msg=<%=System.currentTimeMillis()%>
原文地址:https://www.cnblogs.com/jifeng/p/2225595.html