baidu是如何AJAX跨域的
最近做个人网站遇到AJAX跨子域名的问题。
偶尔看到baidu的通行证处理都是在二级域名passport.baidu.com中处理的,
但是baidu很多地方登录都好像是用ajax处理的,他是怎么做的呢?研究了一下,发现一个小技巧。
不防让大家也借鉴一下。
在http://zhidao.baidu.com/ 未登录用户回答问题时会用iframe调用http://zhidao.baidu.com/userlogin.html
userlogin.html有下面的javascript
<SCRIPT LANGUAGE="JavaScript">
document.domain="baidu.com";
<!--
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";
相关文档:
最近网上提的很多的一个新概念就是 AJAX 了, 那么, AJAX 是什么呢? 以下内容引用网上资料:
AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术。它有机地包含了以下几种技术:
Ajax(Asynchronous JavaScript + XML)的定义
基于 web标准(sta ......
<
script language=
"javascript"
>
$(
document)
.ready
(
function
(
)
{
$(
'#send_ajax'
)
.click
(
function
(
)
{
var
params=
$(
'input'
)
.serialize
(
)
;
//序列化表单的值
$.ajax
(
{
url:
'ajax_json.php'
,
//后台处 ......
在构造url是用javascript自带的encodeURIComponent方法将参数进行编码,下面是我的代码
var url = "handel.jsp?name="+encodeURIComponent(document.form1.name.value);
httpRequest = createHttpRequest();
httpReque ......
var xmlhttp;
function verify()
{
//2.创建XmlHttpRequest对象
//这是XmlHttpRequest对象五步中使用最复杂的一步
//需要针对IE和其他类型的浏览器建立这个对象的不同方式写不同的代码
if(window.XMLHttpRequest)
{
......